国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

IOS自帶Email的兩種方法實例詳解

2020-07-26 02:47:02
字體:
來源:轉載
供稿:網友

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];  }  

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿拉善左旗| 孙吴县| 商丘市| 突泉县| 手机| 垦利县| 邵阳市| 金寨县| 仁怀市| 城固县| 西安市| 卢氏县| 府谷县| 卓尼县| 平舆县| 灌阳县| 杂多县| 故城县| 东港市| 清苑县| 霸州市| 建阳市| 永春县| 鸡东县| 乐都县| 措勤县| 胶州市| 湟源县| 海门市| 巴中市| 乌拉特前旗| 庆城县| 奈曼旗| 遂川县| 望城县| 宝丰县| 麻阳| 岳西县| 博爱县| 峨边| 积石山|