打电话
都会弹框提示用户
第一种方式
let url = URL(string: "tel://10010")
UIApplication.shared.openURL(url!)
- 第二种方式
- 注意:属于私有api,可能会被拒
let url = URL(string: "telprompt://10010")
UIApplication.shared.openURL(url!)
- 第三种方式
- 必须要添加到视图上,不然没有反应
- 不要设置frame,否则会出现遮挡问题
let url = URL(string: "tel://10010")
let webView = UIWebView()
webView.loadRequest(URLRequest(url: url!))
view.addSubview(webView)
发短信
// 跳转到发短信页面,但是无法指定发短信的内容,只能指定一个发送的对象
let url = URL(string: "sms://10010")
UIApplication.shared.openURL(url!)
import MessageUI
// 跳转到发短信页面,指定发送内容,可以指定多个发送的对象,发送之后有结果回掉
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// 短信控制器
let MCVC = MFMessageComposeViewController()
/// 短信内容
MCVC.body = "短信内容"
// 代理
MCVC.messageComposeDelegate = self
// 收件人列表
MCVC.recipients = ["10010", "10086"]
self.present(MCVC, animated: true, completion: nil)
}
extension ViewController : MFMessageComposeViewControllerDelegate {
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
/// 返回短信界面
controller.dismiss(animated: true, completion: nil)
switch result {
case .cancelled:
print("取消发送")
case .sent:
print("已经发送")
case .failed:
print("发送失败")
}
}
}
发邮件
let url = URL(string: "mailto://10010@qq.com")
UIApplication.shared.openURL(url!)
import MessageUI
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let MCVC = MFMailComposeViewController()
/// 内容
MCVC.setMessageBody("邮箱内容", isHTML: false)
// 代理
MCVC.mailComposeDelegate = self
// 收件人列表
MCVC.setToRecipients(["10010@qq.com","10086@qq.com"])
self.present(MCVC, animated: true, completion: nil)
}
}
extension ViewController : MFMailComposeViewControllerDelegate {
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
switch result {
case .cancelled:
print("取消发送")
case .sent:
print("已经发送")
case .failed:
print("发送失败")
case .saved:
print("已保存")
}
}
}
应用之间的跳转
- 示例 A跳转到B
//A
let url = URL(string: "cool:com.test.b")
UIApplication.shared.openURL(url!)
应用评分
- 模拟器没反应,使用真机
let appID = "1338674422"
let url = URL(string: "itms-apps://itunes.apple.com/cn/app/id\(appID)?mt=8")
UIApplication.shared.openURL(url!)