发送邮件
1、引入头文件
import MessageUI
2、发送邮件
func sendMail(_ email: String) {
if #available(iOS 13.0, *) {
if (MFMailComposeViewController.canSendMail() == true) {
let mailComposer = MFMailComposeViewController()
mailComposer.mailComposeDelegate = self
mailComposer.setToRecipients([email])
mailComposer.setCcRecipients([""])
mailComposer.setBccRecipients([""])
mailComposer.setSubject("")
mailComposer.setMessageBody("", isHTML: false)
self.present(mailComposer, animated: true)
}
}else {
let url: NSURL = NSURL(string: email) ?? NSURL()
if (UIApplication.shared.canOpenURL(url as URL) == true) {
UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
}
}
}
3、实现代理MFMailComposeViewControllerDelegate、UINavigationControllerDelegate
extension ViewController: MFMailComposeViewControllerDelegate, UINavigationControllerDelegate {
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
switch result {
case .cancelled:
print("取消发送")
case .saved:
print("保存草稿文件")
case .sent:
print("发送成功")
case .failed:
print("发送失败")
default:
break
}
self.dismiss(animated: true, completion: nil)
}
}
打电话
1、拨打完电话回不到原来的应用,会停留在通讯录里,直接拨打,不弹出提示
let url: NSURL = NSURL(string: "tel:" + telephone)!
UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
2、打完电话后还会回到原来的程序,也会弹出提示
let url: NSURL = NSURL(string: "tel:" + telephone)!
let urlRequest = URLRequest(url: url as URL, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 30)
let webView = WKWebView()
webView.load(urlRequest)
self.view.addSubview(webView)
3、打完电话后还会回到原来的程序,也会弹出提示
let url: NSURL = NSURL(string: "telprompt://" + telephone)!
UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)