最近折騰ios的一個郵件分享和SMS簡訊分享功能,本來是很簡單的的功能,但是我發現開啟的郵件介面和簡訊介面一直不響應navigationController上面的按鈕,感覺很奇怪的,最後發現,需要在開啟的這些介面的委託實現方法中,把開啟的視圖關閉,說的有點彆扭吧! 我發一段代碼出來,大家可能會明白的!
注意:如果想調用傳送簡訊和郵件的功能,必須要引入一個庫:MessageUI.framework
還需要import一些類:
#import <MessageUI/MessageUI.h>#import <MessageUI/MFMailComposeViewController.h>
如下代碼是ios傳送簡訊的代碼:
-(void)sendSMSComposerSheet{ MFMessageComposeViewController *messageCtrl = [[MFMessageComposeViewController alloc] init]; messageCtrl.messageComposeDelegate = self; messageCtrl.body = @"share ...."; [self presentModalViewController:messageCtrl animated:YES];}
這段代碼實現的委託就:MFMessageComposeViewControllerDelegate
可是My Code中也實現了委託,代碼如下:
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{ }
但是開啟簡訊介面是沒問題的,關鍵是點擊簡訊介面的取消按鈕沒有相應,最後我想了一下,估計要在委託的實現方法中關閉開啟的視圖,結果的確如此!
以上代碼修改為:
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{ [controller dismissModalViewControllerAnimated:YES];}
或者:
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{ [controller dismissViewControllerAnimated:YES completion:^{}];}
兩種方式都可以的,這樣就能解決ios使用presentModalViewController開啟viewController,相應viewcongroller上面的相關按鈕了!
同時在附加上 ios發送郵件的代碼:
-(void)sendMailComposerSheet{ MFMailComposeViewController *mailCtrl = [[MFMailComposeViewController alloc] init]; mailCtrl.mailComposeDelegate = self; [mailCtrl setSubject:@"郵件標題"]; [mailCtrl setMessageBody:@"郵件內容" isHTML:NO]; [self presentModalViewController:mailCtrl animated:YES];}
相關的委託:MFMailComposeViewControllerDelegate
介面的處理
-(void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{ [controller dismissModalViewControllerAnimated:YES];}