注意:如果想调用发送短信和邮件的功能,必须要引入一个库:MessageUI.framework
还需要import一些类:
#import <MessageUI/MessageUI.h> #import <MessageUI/MFMailComposeViewController.h>
-(void)sendSMSComposerSheet { MFMessageComposeViewController *messageCtrl = [[MFMessageComposeViewController alloc] init]; messageCtrl.messageComposeDelegate = self; messageCtrl.body = @"share ...."; [self presentModalViewController:messageCtrl animated:YES]; }
这段代码实现的委托就:MFMessageComposeViewControllerDelegate
可是我的代码中也实现了委托,代码如下:
- (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]; }