我在使用最新的 Xcode
和 Swift
版本。
我在使用下面的代码来启动一个屏幕来写一封邮件。
UIApplication.shared.open(URL(string: "mailto:[email protected]")!)
这段代码打开Apple邮件应用,创建一个新的邮件,然后写下... [email protected]
进入 To:
字段。
有时你会看到这个 "写一封新邮件 "窗口在你发起的应用中以覆盖的方式打开,而苹果邮件应用却没有被打开。
我怎么能达到这个目的呢?
如果你想从应用程序内部发送电子邮件,你可以看看下面的代码。MFMailComposeViewController
你可以简单地实例化这个视图控制器,将字段设置为subject、cc...,然后呈现出来。
摘自文档。
if !MFMailComposeViewController.canSendMail() {
print("Mail services are not available")
return
}
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
// Configure the fields of the interface.
composeVC.setToRecipients(["[email protected]"])
composeVC.setSubject("Hello!")
composeVC.setMessageBody("Hello from California!", isHTML: false)
// Present the view controller modally.
self.present(composeVC, animated: true, completion: nil)
func mailComposeController(controller: MFMailComposeViewController,
didFinishWithResult result: MFMailComposeResult, error: NSError?) {
// Check the result or perform other tasks.
// Dismiss the mail compose view controller.
controller.dismiss(animated: true, completion: nil)
}