您好,登錄后才能下訂單哦!
IOS自帶Email的兩種方法實例詳解
IOS系統框架提供的兩種發送Email的方法:openURL 和 MFMailComposeViewController。借助這兩個方法,我們可以輕松的在應用里加入如用戶反饋這類需要發送郵件的功能。
1.openURL
使用openURL調用系統郵箱客戶端是我們在IOS3.0以下實現發郵件功能的主要手段。我們可以通過設置url里的相關參數來指定郵件的內容,不過其缺點很明顯,這樣的過程會導致程序暫時退出。下面是使用openURL來發郵件的一個小例子:
#pragma mark - 使用系統郵件客戶端發送郵件
-(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"]; //添加郵件內容 [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中。通過調用
MFMailComposeViewController,可以把郵件發送窗口集成到我們的應用里,發送郵件就不需要退出程序了。
MFMailComposeViewController的使用方法:
1.項目中引入MessageUI.framework;
2.在使用的文件中導入MFMailComposeViewController.h頭文件;
3.實現MFMailComposeViewControllerDelegate,處理郵件發送事件;
4.調出郵件發送窗口前先使用MFMailComposeViewController里的“+ (BOOL)canSendMail”方法檢查用戶是否設置了郵件賬戶;
5.初始化MFMailComposeViewController,構造郵件體
// // 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 - 在應用內發送郵件 //激活郵件功能 - (void)sendMailInApp { Class mailClass = (NSClassFromString(@"MFMailComposeViewController")); if (!mailClass) { [self alertWithMessage:@"當前系統版本不支持應用內發送郵件功能,您可以使用mailto方法代替"]; return; } if (![mailClass canSendMail]) { [self alertWithMessage:@"用戶沒有設置郵件賬戶"]; return; } [self displayMailPicker]; } //調出郵件發送窗口 - (void)displayMailPicker { MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init]; mailPicker.mailComposeDelegate = self; //設置主題 [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 //關于mimeType:http://www.iana.org/assignments/media-types/index.html [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"]; //添加一個pdf附件 NSString *file = [self fullBundlePathFromRelativePath:@"高質量C++編程指南.pdf"]; NSData *pdf = [NSData dataWithContentsOfFile:file]; [mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"高質量C++編程指南.pdf"]; NSString *emailBody = @"<font color='red'>eMail</font> 正文"; [mailPicker setMessageBody:emailBody isHTML:YES]; [self presentModalViewController: mailPicker animated:YES]; [mailPicker release]; } #pragma mark - 實現 MFMailComposeViewControllerDelegate - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { //關閉郵件發送窗口 [self dismissModalViewControllerAnimated:YES]; NSString *msg; switch (result) { case MFMailComposeResultCancelled: msg = @"用戶取消編輯郵件"; break; case MFMailComposeResultSaved: msg = @"用戶成功保存郵件"; break; case MFMailComposeResultSent: msg = @"用戶點擊發送,將郵件放到隊列中,還沒發送"; break; case MFMailComposeResultFailed: msg = @"用戶試圖保存或者發送郵件失敗"; break; default: msg = @""; break; } [self alertWithMessage:msg]; }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。