IOS自帶Email的兩種方法實(shí)例詳解
IOS自帶Email的兩種方法實(shí)例詳解
IOS系統(tǒng)框架提供的兩種發(fā)送Email的方法:openURL 和 MFMailComposeViewController。借助這兩個方法,我們可以輕松的在應(yīng)用里加入如用戶反饋這類需要發(fā)送郵件的功能。
1.openURL
使用openURL調(diào)用系統(tǒng)郵箱客戶端是我們在IOS3.0以下實(shí)現(xiàn)發(fā)郵件功能的主要手段。我們可以通過設(shè)置url里的相關(guān)參數(shù)來指定郵件的內(nèi)容,不過其缺點(diǎn)很明顯,這樣的過程會導(dǎo)致程序暫時退出。下面是使用openURL來發(fā)郵件的一個小例子:
#pragma mark - 使用系統(tǒng)郵件客戶端發(fā)送郵件
-(void)launchMailApp
{
NSMutableString *mailUrl = [[[NSMutableString alloc]init]autorelease];
//添加收件人
NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];
[mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]];
//添加抄送
NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
[mailUrl appendFormat:@"?cc=%@", [ccRecipients componentsJoinedByString:@","]];
//添加密送
NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];
[mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]];
//添加主題
[mailUrl appendString:@"&subject=my email"];
//添加郵件內(nèi)容
[mailUrl appendString:@"&body=<b>email</b> body!"];
NSString* email = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];
}
2.MFMailComposeViewController
MFMailComposeViewController是在IOS3.0新增的一個接口,它在MessageUI.framework中。通過調(diào)用
MFMailComposeViewController,可以把郵件發(fā)送窗口集成到我們的應(yīng)用里,發(fā)送郵件就不需要退出程序了。
MFMailComposeViewController的使用方法:
1.項目中引入MessageUI.framework;
2.在使用的文件中導(dǎo)入MFMailComposeViewController.h頭文件;
3.實(shí)現(xiàn)MFMailComposeViewControllerDelegate,處理郵件發(fā)送事件;
4.調(diào)出郵件發(fā)送窗口前先使用MFMailComposeViewController里的“+ (BOOL)canSendMail”方法檢查用戶是否設(shè)置了郵件賬戶;
5.初始化MFMailComposeViewController,構(gòu)造郵件體
// // ViewController.h // MailDemo // // Created by LUOYL on 12-4-4. // Copyright (c) 2012年 http://luoyl.info. All rights reserved. // #import <UIKit/UIKit.h> #import <MessageUI/MFMailComposeViewController.h> @interface ViewController : UIViewController<MFMailComposeViewControllerDelegate> @end
#pragma mark - 在應(yīng)用內(nèi)發(fā)送郵件
//激活郵件功能
- (void)sendMailInApp
{
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (!mailClass) {
[self alertWithMessage:@"當(dāng)前系統(tǒng)版本不支持應(yīng)用內(nèi)發(fā)送郵件功能,您可以使用mailto方法代替"];
return;
}
if (![mailClass canSendMail]) {
[self alertWithMessage:@"用戶沒有設(shè)置郵件賬戶"];
return;
}
[self displayMailPicker];
}
//調(diào)出郵件發(fā)送窗口
- (void)displayMailPicker
{
MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
mailPicker.mailComposeDelegate = self;
//設(shè)置主題
[mailPicker setSubject: @"eMail主題"];
//添加收件人
NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];
[mailPicker setToRecipients: toRecipients];
//添加抄送
NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
[mailPicker setCcRecipients:ccRecipients];
//添加密送
NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];
[mailPicker setBccRecipients:bccRecipients];
// 添加一張圖片
UIImage *addPic = [UIImage imageNamed: @"Icon@2x.png"];
NSData *imageData = UIImagePNGRepresentation(addPic); // png
//關(guān)于mimeType:http://www.iana.org/assignments/media-types/index.html
[mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"];
//添加一個pdf附件
NSString *file = [self fullBundlePathFromRelativePath:@"高質(zhì)量C++編程指南.pdf"];
NSData *pdf = [NSData dataWithContentsOfFile:file];
[mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"高質(zhì)量C++編程指南.pdf"];
NSString *emailBody = @"<font color='red'>eMail</font> 正文";
[mailPicker setMessageBody:emailBody isHTML:YES];
[self presentModalViewController: mailPicker animated:YES];
[mailPicker release];
}
#pragma mark - 實(shí)現(xiàn) MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
//關(guān)閉郵件發(fā)送窗口
[self dismissModalViewControllerAnimated:YES];
NSString *msg;
switch (result) {
case MFMailComposeResultCancelled:
msg = @"用戶取消編輯郵件";
break;
case MFMailComposeResultSaved:
msg = @"用戶成功保存郵件";
break;
case MFMailComposeResultSent:
msg = @"用戶點(diǎn)擊發(fā)送,將郵件放到隊列中,還沒發(fā)送";
break;
case MFMailComposeResultFailed:
msg = @"用戶試圖保存或者發(fā)送郵件失敗";
break;
default:
msg = @"";
break;
}
[self alertWithMessage:msg];
}
相關(guān)文章
詳解iOS開發(fā)中的轉(zhuǎn)場動畫和組動畫以及UIView封裝動畫
這篇文章主要介紹了iOS開發(fā)中的轉(zhuǎn)場動畫和組動畫以及UIView封裝動畫,主要用到了CAAnimation類和UIView類,需要的朋友可以參考下2015-11-11
iOS中利用CAEmitterLayer實(shí)現(xiàn)粒子動畫詳解
粒子效果應(yīng)該對現(xiàn)在很多人來說并不陌生,我們之前也分享了一些相關(guān)文章,下面這篇文章主要給大家介紹了關(guān)于iOS中利用CAEmitterLayer實(shí)現(xiàn)粒子動畫的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友們下面來一起看看吧。2017-06-06
解決Alamofire庫在iOS7下設(shè)置Head無效的問題
本文主要介紹Alamofire庫在iOS下設(shè)置Head,這里通過代碼實(shí)例解決不同版本的IOS系統(tǒng)出現(xiàn)的問題,有需要的小伙伴可以參考下2016-07-07
IOS 中NSUserDefaults讀取和寫入自定義對象的實(shí)現(xiàn)方法
這篇文章主要介紹了IOS 中NSUserDefaults讀取和寫入自定義對象的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文大家能夠理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09

