iOS发送邮件&打电话

该文章介绍了如何在iOS应用中实现发送邮件的功能,使用MFMailComposeViewController进行邮件发送,并处理发送结果。同时,文章也讨论了两种拨打电话的方法,包括直接拨打和通过WKWebView,以及它们对应用行为的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

发送邮件

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CConch

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值