现在在开发中应该会经常遇到点击邀请 发送短信给指定人员
点击邀请 弹出短信界面 并发送指定内容,手动点击发送即可
如果只要打开短信界面,并显示号码很简单,一句代码
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@sms://13666666666]];
但要指定短信内容 需要如下方法
1.先引入头文件
#import <MessageUI/MFMessageComposeViewController.h>
2.设置代理
MFMessageComposeViewControllerDelegate
3.实现代理方法
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self.mobileContactsVC dismissViewControllerAnimated:YES completion:nil];
if (result == MessageComposeResultCancelled)
{
NSLog(@"Message cancelled");
}
else if (result == MessageComposeResultSent)
{
NSLog(@"Message sent");
}
else
{
NSLog(@"Message failed");
}
}
- 调用方法
/**
* 指定内容 打开SMS
*
* @param bodyOfMessage 要发送的内容
* @param recipients 要发送的号码
*/
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
{
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
controller.body = bodyOfMessage;
controller.recipients = recipients;
controller.messageComposeDelegate = self;
[self.mobileContactsVC presentViewController:controller animated:YES completion:nil];
}
}```
只要在需要调用的地方 调用方法即可
[self sendSMS:@"加入工付宝 领取红包金额" recipientList:[NSArray arrayWithObjects:[NSString stringWithFormat:@"%@", self.mobileContactsModel.mobile], nil]];