短信分享方法要導入的頭文件和郵件分享要導入的頭文件同樣:web
#import <MessageUI/MessageUI.h>工具
可是它所遵照的協議卻不相同:
spa
@interface class ()<MFMessageComposeViewControllerDelegate>.net
實現短信分享的方法和協議方法以下所示:3d
1.在.m文件中實現的方法爲:
orm
-(void)shareMessage{get
//判斷是否發送信息string
BOOL canSendMessage=[MFMessageComposeViewController canSendText];it
if (canSendMessage) {io
//建立短信視圖控制器
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;
}
}