Email 的兩種方法
阿新 • • 發佈:2018-11-04
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: @"[email protected]"];
- [mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@
- //新增抄送
- NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];
- [mailUrl appendFormat:@
- //新增密送
- NSArray *bccRecipients = [NSArray arrayWithObjects:@"[email protected]", nil];
- [mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]];
- //新增主題
- [mailUrl appendString:@"&subject=my subject"];
- //新增郵件內容
- [mailUrl appendString:@"&body=email 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: @"[email protected]"];
- [mailPicker setToRecipients: toRecipients];
- //新增抄送
- NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];
- [mailPicker setCcRecipients:ccRecipients];
- //新增密送
- NSArray *bccRecipients = [NSArray arrayWithObjects:@"[email protected]", nil];
- [mailPicker setBccRecipients:bccRecipients];
- // 新增一張圖片
- UIImage *addPic = [UIImage imageNamed: @"[email protected]"];
- 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];
- }