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

首頁 > 學院 > 開發設計 > 正文

iOS-----MFMessageCompose和MFMailComposeViewController的使用方法

2019-11-14 17:52:53
字體:
來源:轉載
供稿:網友

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的類方法進行判斷之后,接下來就可按如下步驟發送郵件.

  1. 創建MFMailComposeViewController對象
  2. 為MFMailComposeViewController設置toRecipients: (接受NSArray作為屬性值,用于設置多個收件人地址)、ccRecipients:(接受NSArray作為屬性值,用于設置多個抄送人地址)、bccRecipients:(接收NSArray作為屬性值,用于設置多個密送人地址)、subject(設置郵件主題),還可通過setMessageBody:isHTML:方法設置郵件正文,通過addAttachmentData:mimeType:filename:方法添加附件.
  3. 為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協議,并實現了該協議中的方法----該方法負責處理發送郵件的結果。

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

 

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 交口县| 商河县| 马关县| 师宗县| 威远县| 明水县| 钟祥市| 武川县| 临猗县| 招远市| 永修县| 泰安市| 运城市| 湘西| 普兰县| 桂阳县| 许昌市| 深泽县| 大丰市| 镇原县| 曲阳县| 武强县| 南木林县| 淮南市| 丰顺县| 竹北市| 科技| 崇义县| 昌图县| 潜江市| 东源县| 泰兴市| 东城区| 岫岩| 同德县| 南康市| 石屏县| 贵南县| 布尔津县| 荃湾区| 凤山县|