短信分享方法要导入的头文件和邮件分享要导入的头文件一样:
#import <MessageUI/MessageUI.h>
但是它所遵守的协议却不相同:
class ()<MFMessageComposeViewControllerDelegate>
实现短信分享的方法和协议方法如下所示:
1.在.m文件中实现的方法为:
-(void)shareMessage{
//判断是否发送信息
BOOL canSendMessage=[MFMessageComposeViewController canSendText];
if (canSendMessage) {
//创建短信视图控制器
MFMessageComposeViewController *messageComposeViewController=[[MFMessageComposeViewController alloc]init];
NSString * bodyString = [NSString stringWithFormat:@"我制作了一个名为\"%@\"的旅拍,赶快来欣赏吧!\n\n请猛戳链接:\n%@\n\n旅拍-最方便的拍照记录分享工具",_webInfo.title,_webInfo.webUrl];
[messageComposeViewController setBody:bodyString];
messageComposeViewController.messageComposeDelegate=self;
[self presentViewController:messageComposeViewController animated:YES completion:nil];
}
}
2.实现的协议方法为:
#pragma mark - MFMessageComposeViewControllerDelegate
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
switch (result) {
case MessageComposeResultCancelled:
NSLog(@"取消发送短信");
break;
case MessageComposeResultFailed:
NSLog(@"发送短信失败");
break;
case MessageComposeResultSent:
NSLog(@"发送短信成功");
break;
default:
break;
}
}