注意:如果想调用发送短信和邮件的功能,必须要引入一个库: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:^{}]; }
-(void)sendMailComposerSheet { MFMailComposeViewController *mailCtrl = [[MFMailComposeViewController alloc] init]; mailCtrl.mailComposeDelegate = self; [mailCtrl setSubject:@"邮件标题"]; [mailCtrl setMessageBody:@"邮件内容" isHTML:NO]; [self presentModalViewController:mailCtrl animated:YES]; }
-(void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { [controller dismissModalViewControllerAnimated:YES]; }