iOS调用系统的发短信 功能可以分为两种:1、程序外调用系统发短信。2、程序内调用系统发短信。第二种的好处是用户发短信之后还可以回到APP。
1、程序外调用系统发短信(在swift3.0下)
这个方法其实很简单,直接调用openURL即可:
[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:@"sms://13888888888"]];
2、程序内调用系统发短信(在swift3.0下)
1)导入MessageUI.framework,并引入头文件:
import MessageUI
2)实现代理方法MFMessageComposeViewControllerDelegate
//MARK: //////////Delegate///////////
//消息传送后的回调
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
self.dismiss(animated: true, completion: nil)
switch result {
case MessageComposeResult.sent:
//信息传送成功
break
case MessageComposeResult.failed:
//信息传送失败
break
case MessageComposeResult.cancelled:
//信息被用户取消传送
break
}
}
//参数1:发送短信的电话号数组
//参数2:title为发短信页面标题(测试发现并无卵用,改页面标题仍然为“新信息”,如有人发现如何更改请告知)
//参数3:指定的发送内容
//发送短信方法
func showMessageComposeViewController(phones : NSArray, title : String, body: String){
//判断当前设备是否有发短信功能
if MFMessageComposeViewController.canSendText() {
let controller = MFMessageComposeViewController()
controller.recipients = phones as? [String]
controller.navigationBar.tintColor = UIColor.red
controller.body = body//短信内容
controller.messageComposeDelegate = self//设置委托
self.present(controller, animated: true, completion: nil)
controller.viewControllers.last?.navigationItem.title = title
} else{
print("该设备部支持短信功能, 并作出相应的提醒")
}
}
self.showMessageComposeViewController(phones: [], title: "test", body: "门店邀您参与分享活动标题,赶快来参与吧!http://baidu.com")