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

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

MFMessageCompose 和 MFMailComposeViewControlle

2019-11-09 14:53:36
字體:
供稿:網(wǎng)友

MFMessageCompose 和 MFMailComposeViewController的使用方法

使用MFMessageComposeViewCOntroller發(fā)短信

應(yīng)用想自己提供界面讓用戶輸入短信收件人地址、短信內(nèi)容、主體、附件等短信內(nèi)容,則可使用MFMessageComposeViewController來發(fā)送短信,它也是一個視圖控制器,繼承UINavigationController.

MFMessageComposeViewController提供了如下類方法判斷iOS設(shè)備是否支持發(fā)送短信.

+ canSendText:

該iOS設(shè)備是否支持發(fā)送文本短信.

+ canSendAttachments:

該iOS設(shè)備是否支持發(fā)送帶附件的短信

+ canSendSubject:

該iOS設(shè)備是否支持發(fā)送帶標題的短信

程序使用MFMessageComposeViewController的類方法進行判斷之后,接下來就可按如下步驟發(fā)送短信.

1

創(chuàng)建MFMessageComposeViewController對象

2

為MFMessageComposeViewController設(shè)置recipients(接受NSArray作為屬性值,用于設(shè)置多個收件人號碼)、subject(設(shè)置短信主題)、body(設(shè)置短信內(nèi)容)、attachments(接受NSArray作為屬性值,用于設(shè)置多個附件)等屬性

3

為MFMessageComposeViewController設(shè)置messageComposeDelegate,該屬性值必須是一個實現(xiàn)MFMessageComposeViewControllerDelegate協(xié)議的對象.該協(xié)議中定義了一個必須實現(xiàn)的messageComposeViewComtroller:didFinishWithResult:方法,該方法負責處理短信的發(fā)送結(jié)果.

  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        // 如果能發(fā)送文本信息 28  29       if([MFMessageComposeViewController canSendText]) 30  31       { 32  33 // 創(chuàng)建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 // 設(shè)置收件人,此處可通過NSArray集合指定多個收件人 44  45 picker.recipients = [NSArray arrayWithObject:destStr]; 46  47 // 設(shè)置短信內(nèi)容 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協(xié)議中的方法,負責處理短信的發(fā)送結(jié)果 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:@”結(jié)果: 短信被取消發(fā)送”]; 82  83       break; 84  85   case MessageComposeResultSent: 86  87       [self showAlert:@”結(jié)果: 發(fā)送成功”]; 88  89       break; 90  91   case MessageComposeResultFailed: 92  93       [self showAlert:@”結(jié)果: 發(fā)送失敗”]; 94  95         break; 96  97    default: 98  99       [self showAlert:@”結(jié)果: 沒有發(fā)送短信”];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:@”發(fā)送結(jié)果” message:msg delegate:nil cancelButtonTitle:@”確 定” otherButtonTitles:nil] show];114 115 }116 117 @end

 

創(chuàng)建了一個MFMessageComposeViewController對象,并為該對象設(shè)置了recipents(收件人)、body(短信內(nèi)容),并將該視圖控制器本身設(shè)為它的messageComposeDelegate,因此該視圖控制器類實現(xiàn)了MFMessageComposeViewControllerDelegate協(xié)議,并實現(xiàn)該協(xié)議中的方法----該方法負責處理發(fā)送短信的結(jié)果。

 

   

使用MFMailComposeViewController發(fā)送郵件

MFMailComposeViewController與MFMessageComposeViewController的用法非常相似,只是功能不同而已------MFMailComposeViewController用于發(fā)送郵件。

     MFMailComposeViewController提供了如下類方法判斷iOS設(shè)備是否支持發(fā)送郵件。

        + canSendMail: 該iOS設(shè)備是否支持發(fā)送郵件.

程序使用MFMailComposeViewController的類方法進行判斷之后,接下來就可按如下步驟發(fā)送郵件.

創(chuàng)建MFMailComposeViewController對象為MFMailComposeViewController設(shè)置toRecipients: (接受NSArray作為屬性值,用于設(shè)置多個收件人地址)、ccRecipients:(接受NSArray作為屬性值,用于設(shè)置多個抄送人地址)、bccRecipients:(接收NSArray作為屬性值,用于設(shè)置多個密送人地址)、subject(設(shè)置郵件主題),還可通過setMessageBody:isHTML:方法設(shè)置郵件正文,通過addAttachmentData:mimeType:filename:方法添加附件.為MFMailComposeViewController設(shè)置mailComposeDelegate,該屬性值必須是一個實現(xiàn)MFMailComposeViewControllerDelegate協(xié)議的對象.該協(xié)議中定義了一個必須實現(xiàn)的mailComposeController:didFinishWithResult:error:方法,該方法負責處理郵件的發(fā)送結(jié)果.

  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   // 獲取界面上用戶輸入的內(nèi)容 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   // 如果能發(fā)送郵件 40  41   if([MFMailComposeViewController canSendMail]) 42  43 { 44  45   // 創(chuàng)建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 // 設(shè)置收件人,此處可通過NSArray集合指定多個收件人 56  57 picker.toRecipients = [NSArray arrayWithObject:toStr]; 58  59 if(ccStr != nil && ccStr.length > 0) 60  61 { 62  63   // 設(shè)置抄送人,此處可通過NSArray集合指定多個抄送人 64  65  picker.ccRecipients = [NSArray arrayWithObject:ccStr]; 66  67 } 68  69 if(bccStr != nil && bccStr.length > 0) 70  71 { 72  73   // 設(shè)置密送人,此處可用過NSArray集合指定多個密送人 74  75   picker.bccRecipients = [NSArray arrayWithObject:bccStr]; 76  77 } 78  79 // 設(shè)置郵件主題 80  81 picker.subject = subjectStr; 82  83 // 設(shè)置郵件正文 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:@”結(jié)果: 郵件被取消發(fā)送”];118 119      break;120 121 case  MFMailComposeResultSent:122 123     [self showAlert:@”結(jié)果: 發(fā)送成功”];124 125     break;126 127 case  MFMailComposeResultFailed:128 129     [self showAlert:@”結(jié)果: 發(fā)送失敗”];130 131     break;132 133 case  MFMailComposeResultSaved:134 135     [self showAlert:@”結(jié)果: 郵件被保存了”];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:@”發(fā)送結(jié)果”  message:msg delegate:nil cancelButtonTitle:@”確定” otherButtonTitles:nil] show];150 151 }152 153 @end

 

// 上面程序中的粗體字代碼創(chuàng)建了一個MFMailComposeViewController對象,并為該對象設(shè)置了toRecipients(收件人地址)、ccRecipients(抄送人地址)、bccRecipients(密送人地址),還調(diào)用了setMessageBody:contentStr isHTML:方法設(shè)置郵件正文,并將該視圖控制器本身設(shè)為它的mailComposeDelegate,因此該視圖控制器類實現(xiàn)MFMailComposeViewControllerDelegate協(xié)議,并實現(xiàn)了該協(xié)議中的方法----該方法負責處理發(fā)送郵件的結(jié)果。

// 編譯、運行該程序(必須在真機中運行,模擬器不支持),在程序界面中輸入收件人地址、抄送地址、密送人地址、郵件主題、郵件正文,然后單擊“發(fā)送”按鈕,將可以看到如下圖所示的界面

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宣武区| 永登县| 营山县| 淮安市| 徐水县| 赣榆县| 历史| 丘北县| 万源市| 晋中市| 孟州市| 泊头市| 西青区| 玛沁县| 乃东县| 桂平市| 禄劝| 阳城县| 永定县| 广河县| 保定市| 嘉峪关市| 阳高县| 昌平区| 通渭县| 永仁县| 乾安县| 杭锦旗| 新蔡县| 鹤壁市| 界首市| 仪陇县| 隆安县| 漠河县| 明水县| 凯里市| 洪江市| 阿瓦提县| 兴城市| 兰考县| 文安县|