IOS自带Email的两种方法实例详解
IOS自带Email的两种方法实例详解
IOS系统框架提供的两种发送Email的方法:openURL和MFMailComposeViewController。借助这两个方法,我们可以轻松的在应用里加入如用户反馈这类需要发送邮件的功能。
1.openURL
使用openURL调用系统邮箱客户端是我们在IOS3.0以下实现发邮件功能的主要手段。我们可以通过设置url里的相关参数来指定邮件的内容,不过其缺点很明显,这样的过程会导致程序暂时退出。下面是使用openURL来发邮件的一个小例子:
#pragmamark-使用系统邮件客户端发送邮件
-(void)launchMailApp { NSMutableString*mailUrl=[[[NSMutableStringalloc]init]autorelease]; //添加收件人 NSArray*toRecipients=[NSArrayarrayWithObject:@"first@example.com"]; [mailUrlappendFormat:@"mailto:%@",[toRecipientscomponentsJoinedByString:@","]]; //添加抄送 NSArray*ccRecipients=[NSArrayarrayWithObjects:@"second@example.com",@"third@example.com",nil]; [mailUrlappendFormat:@"?cc=%@",[ccRecipientscomponentsJoinedByString:@","]]; //添加密送 NSArray*bccRecipients=[NSArrayarrayWithObjects:@"fourth@example.com",nil]; [mailUrlappendFormat:@"&bcc=%@",[bccRecipientscomponentsJoinedByString:@","]]; //添加主题 [mailUrlappendString:@"&subject=myemail"]; //添加邮件内容 [mailUrlappendString:@"&body=emailbody!"]; NSString*email=[mailUrlstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [[UIApplicationsharedApplication]openURL:[NSURLURLWithString:email]]; }
2.MFMailComposeViewController
MFMailComposeViewController是在IOS3.0新增的一个接口,它在MessageUI.framework中。通过调用
MFMailComposeViewController,可以把邮件发送窗口集成到我们的应用里,发送邮件就不需要退出程序了。
MFMailComposeViewController的使用方法:
1.项目中引入MessageUI.framework;
2.在使用的文件中导入MFMailComposeViewController.h头文件;
3.实现MFMailComposeViewControllerDelegate,处理邮件发送事件;
4.调出邮件发送窗口前先使用MFMailComposeViewController里的“+(BOOL)canSendMail”方法检查用户是否设置了邮件账户;
5.初始化MFMailComposeViewController,构造邮件体
// //ViewController.h //MailDemo // //CreatedbyLUOYLon12-4-4. //Copyright(c)2012年http://luoyl.info.Allrightsreserved. // #import#import @interfaceViewController:UIViewController @end
#pragmamark-在应用内发送邮件 //激活邮件功能 -(void)sendMailInApp { ClassmailClass=(NSClassFromString(@"MFMailComposeViewController")); if(!mailClass){ [selfalertWithMessage:@"当前系统版本不支持应用内发送邮件功能,您可以使用mailto方法代替"]; return; } if(![mailClasscanSendMail]){ [selfalertWithMessage:@"用户没有设置邮件账户"]; return; } [selfdisplayMailPicker]; } //调出邮件发送窗口 -(void)displayMailPicker { MFMailComposeViewController*mailPicker=[[MFMailComposeViewControlleralloc]init]; mailPicker.mailComposeDelegate=self; //设置主题 [mailPickersetSubject:@"eMail主题"]; //添加收件人 NSArray*toRecipients=[NSArrayarrayWithObject:@"first@example.com"]; [mailPickersetToRecipients:toRecipients]; //添加抄送 NSArray*ccRecipients=[NSArrayarrayWithObjects:@"second@example.com",@"third@example.com",nil]; [mailPickersetCcRecipients:ccRecipients]; //添加密送 NSArray*bccRecipients=[NSArrayarrayWithObjects:@"fourth@example.com",nil]; [mailPickersetBccRecipients:bccRecipients]; //添加一张图片 UIImage*addPic=[UIImageimageNamed:@"Icon@2x.png"]; NSData*imageData=UIImagePNGRepresentation(addPic);//png //关于mimeType:http://www.iana.org/assignments/media-types/index.html [mailPickeraddAttachmentData:imageDatamimeType:@""fileName:@"Icon.png"]; //添加一个pdf附件 NSString*file=[selffullBundlePathFromRelativePath:@"高质量C++编程指南.pdf"]; NSData*pdf=[NSDatadataWithContentsOfFile:file]; [mailPickeraddAttachmentData:pdfmimeType:@""fileName:@"高质量C++编程指南.pdf"]; NSString*emailBody=@"eMail正文"; [mailPickersetMessageBody:emailBodyisHTML:YES]; [selfpresentModalViewController:mailPickeranimated:YES]; [mailPickerrelease]; } #pragmamark-实现MFMailComposeViewControllerDelegate -(void)mailComposeController:(MFMailComposeViewController*)controllerdidFinishWithResult:(MFMailComposeResult)resulterror:(NSError*)error { //关闭邮件发送窗口 [selfdismissModalViewControllerAnimated:YES]; NSString*msg; switch(result){ caseMFMailComposeResultCancelled: msg=@"用户取消编辑邮件"; break; caseMFMailComposeResultSaved: msg=@"用户成功保存邮件"; break; caseMFMailComposeResultSent: msg=@"用户点击发送,将邮件放到队列中,还没发送"; break; caseMFMailComposeResultFailed: msg=@"用户试图保存或者发送邮件失败"; break; default: msg=@""; break; } [selfalertWithMessage:msg]; }