首页 > 学院 > 开发设计 > 正文

MFMessageCompose 和 MFMailComposeViewControlle

2019-11-09 14:53:36
字体:
来源:转载
供稿:网友

MFMessageCompose 和 MFMailComposeViewController的使用方法

使用MFMessageComposeViewCOntroller发短信

应用想自己提供界面让用户输入短信收件人地址、短信内容、主体、附件等短信内容,则可使用MFMessageComposeViewController来发送短信,它也是一个视图控制器,继承UINavigationController.

MFMessageComposeViewController提供了如下类方法判断iOS设备是否支持发送短信.

+ canSendText:

该iOS设备是否支持发送文本短信.

+ canSendAttachments:

该iOS设备是否支持发送带附件的短信

+ canSendSubject:

该iOS设备是否支持发送带标题的短信

程序使用MFMessageComposeViewController的类方法进行判断之后,接下来就可按如下步骤发送短信.

1

创建MFMessageComposeViewController对象

2

为MFMessageComposeViewController设置recipients(接受NSArray作为属性值,用于设置多个收件人号码)、subject(设置短信主题)、body(设置短信内容)、attachments(接受NSArray作为属性值,用于设置多个附件)等属性

3

为MFMessageComposeViewController设置messageComposeDelegate,该属性值必须是一个实现MFMessageComposeViewControllerDelegate协议的对象.该协议中定义了一个必须实现的messageComposeViewComtroller:didFinishWithResult:方法,该方法负责处理短信的发送结果.

  1 @interface LCViewController()<MFMessageComposeViewControllerDelegate>  2   3 @end  4   5 @implementation LCViewController  6   7 - (void)viewDidLoad  8   9 { 10  11    [super viewDidLoad]; 12  13 } 14  15 - (IBAction)send:(id)sender 16  17 { 18  19    NSString* destStr = self.destField.text; 20  21    NSString* contentStr = self.contentField.text; 22  23    if(destStr != nil && destStr.length>0 && contentStr != nil && destStr.length > 0 ) 24  25    { 26  27        // 如果能发送文本信息 28  29       if([MFMessageComposeViewController canSendText]) 30  31       { 32  33 // 创建MFMessageComposeViewController对象 34  35 MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init]; 36  37 // 为MFMessageComposeViewController对象指定messageComposeDelegate 38  39 picker.messageComposeDelegate = self; 40  41 picker.navigationBar.tintColor = [UIColor blackColor]; 42  43 // 设置收件人,此处可通过NSArray集合指定多个收件人 44  45 picker.recipients = [NSArray arrayWithObject:destStr]; 46  47 // 设置短信内容 48  49 picker.body = contenStr; 50  51 /* 52  53   如果运营商支持,picker还支持指定subjecy(主题)和attachments(附件) 54  55   也可用addAttachmentURL:withAlternateFilename:或addAttachmentData:typeIdentifier:filename:方法添加附件 56  57 */ 58  59 // 显示MFMessageComposeViewController控制器 60  61 [self persentViewController:picker animated:YES completion:nil]; 62  63 }   64  65 } 66  67 } 68  69 //  MFMessageComposeViewControllerDelegate协议中的方法,负责处理短信的发送结果 70  71 - (void)messageComposeViewController:(MFMessageComposeViewController*)controller didFinishWithResult:(MessageComposeResult)result 72  73 { 74  75 switch(result) 76  77 { 78  79   case MessageComposeResultCancelled: 80  81       [self showAlert:@”结果: 短信被取消发送”]; 82  83       break; 84  85   case MessageComposeResultSent: 86  87       [self showAlert:@”结果: 发送成功”]; 88  89       break; 90  91   case MessageComposeResultFailed: 92  93       [self showAlert:@”结果: 发送失败”]; 94  95         break; 96  97    default: 98  99       [self showAlert:@”结果: 没有发送短信”];100 101     break;102 103 }104 105 [self dismissViewControllerAnimated:YES completion:nil];106 107 }108 109 - (void)showAlert:(NSString *)msg110 111 {112 113 [[[UIAlertView alloc] initWithTitle:@”发送结果” message:msg delegate:nil cancelButtonTitle:@”确 定” otherButtonTitles:nil] show];114 115 }116 117 @end

 

创建了一个MFMessageComposeViewController对象,并为该对象设置了recipents(收件人)、body(短信内容),并将该视图控制器本身设为它的messageComposeDelegate,因此该视图控制器类实现了MFMessageComposeViewControllerDelegate协议,并实现该协议中的方法----该方法负责处理发送短信的结果。

 

   

使用MFMailComposeViewController发送邮件

MFMailComposeViewController与MFMessageComposeViewController的用法非常相似,只是功能不同而已------MFMailComposeViewController用于发送邮件。

     MFMailComposeViewController提供了如下类方法判断iOS设备是否支持发送邮件。

        + canSendMail: 该iOS设备是否支持发送邮件.

程序使用MFMailComposeViewController的类方法进行判断之后,接下来就可按如下步骤发送邮件.

创建MFMailComposeViewController对象为MFMailComposeViewController设置toRecipients: (接受NSArray作为属性值,用于设置多个收件人地址)、ccRecipients:(接受NSArray作为属性值,用于设置多个抄送人地址)、bccRecipients:(接收NSArray作为属性值,用于设置多个密送人地址)、subject(设置邮件主题),还可通过setMessageBody:isHTML:方法设置邮件正文,通过addAttachmentData:mimeType:filename:方法添加附件.为MFMailComposeViewController设置mailComposeDelegate,该属性值必须是一个实现MFMailComposeViewControllerDelegate协议的对象.该协议中定义了一个必须实现的mailComposeController:didFinishWithResult:error:方法,该方法负责处理邮件的发送结果.

  1 @interface LCViewController()<MFMailComposeViewControllerDelegate>  2   3 @end  4   5 @implementation LCViewController  6   7 -(void)viewDidLoad  8   9 { 10  11    [super viewDidLoad]; 12  13 } 14  15 -(IBAction)sendMail:(id)sender 16  17 { 18  19   // 获取界面上用户输入的内容 20  21   NSString* toStr = self.toField.text;// 收件人地址 22  23   NSString* ccStr = self.ccField.text;// 抄送人地址 24  25   NSString* bccStr = self.bccField.text;// 密送人地址 26  27   NSString* subjectStr = self.subjectField.text;// 邮件主题 28  29 NSString* contentStr = self.contentField.text;// 邮件正文 30  31 if(toStr != nil && toStr.length > 0 32  33 && subjectStr != nil && subjectStr.length > 0 34  35 && contentStr != nil && contentStr.length > 0) 36  37 { 38  39   // 如果能发送邮件 40  41   if([MFMailComposeViewController canSendMail]) 42  43 { 44  45   // 创建MFMailComposeViewController对象 46  47  MFMailComposeViewController *picker = [[MFMailComposeViewController alloc]  init]; 48  49 // 为 MFMailComposeViewController对象指定mailComposeDelegate 50  51 picker.mailComposeDelegate = self; 52  53 picker.navigationBar.tintColor = [UIColor blackColor]; 54  55 // 设置收件人,此处可通过NSArray集合指定多个收件人 56  57 picker.toRecipients = [NSArray arrayWithObject:toStr]; 58  59 if(ccStr != nil && ccStr.length > 0) 60  61 { 62  63   // 设置抄送人,此处可通过NSArray集合指定多个抄送人 64  65  picker.ccRecipients = [NSArray arrayWithObject:ccStr]; 66  67 } 68  69 if(bccStr != nil && bccStr.length > 0) 70  71 { 72  73   // 设置密送人,此处可用过NSArray集合指定多个密送人 74  75   picker.bccRecipients = [NSArray arrayWithObject:bccStr]; 76  77 } 78  79 // 设置邮件主题 80  81 picker.subject = subjectStr; 82  83 // 设置邮件正文 84  85 [picker setMessageBody:contentStr isHTML:NO]; 86  87 // 显示MFMailComposeViewController控制器 88  89 [self persentViewController:picker animated:YES completion:nil]; 90  91 } 92  93 } 94  95 } 96  97 -(IBAction)finishEdit:(id)sender 98  99 {100 101   [sender resignFirstResponder];102 103 }104 105 -(void)mailComposeController:(MFMailComposeViewController*)controller106 107   didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error108 109 {110 111   switch(result)112 113   {114 115 case  MFMailComposeResultCancelled:116 117      [self showAlert:@”结果: 邮件被取消发送”];118 119      break;120 121 case  MFMailComposeResultSent:122 123     [self showAlert:@”结果: 发送成功”];124 125     break;126 127 case  MFMailComposeResultFailed:128 129     [self showAlert:@”结果: 发送失败”];130 131     break;132 133 case  MFMailComposeResultSaved:134 135     [self showAlert:@”结果: 邮件被保存了”];136 137     break;138 139 }140 141 [self dismissViewControllerAnimated:YES completion:nil];142 143 }144 145 -(void)showAlert:(NSString *)msg146 147 {148 149   [ [ [ UIAlertView alloc] initWithTitle:@”发送结果”  message:msg delegate:nil cancelButtonTitle:@”确定” otherButtonTitles:nil] show];150 151 }152 153 @end

 

// 上面程序中的粗体字代码创建了一个MFMailComposeViewController对象,并为该对象设置了toRecipients(收件人地址)、ccRecipients(抄送人地址)、bccRecipients(密送人地址),还调用了setMessageBody:contentStr isHTML:方法设置邮件正文,并将该视图控制器本身设为它的mailComposeDelegate,因此该视图控制器类实现MFMailComposeViewControllerDelegate协议,并实现了该协议中的方法----该方法负责处理发送邮件的结果。

// 编译、运行该程序(必须在真机中运行,模拟器不支持),在程序界面中输入收件人地址、抄送地址、密送人地址、邮件主题、邮件正文,然后单击“发送”按钮,将可以看到如下图所示的界面

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表