在自己的应用中打开邮件界面,而不是在邮件应用中打开。

问题描述 投票:0回答:1

我在使用最新的 XcodeSwift 版本。

我在使用下面的代码来启动一个屏幕来写一封邮件。

UIApplication.shared.open(URL(string: "mailto:[email protected]")!)

这段代码打开Apple邮件应用,创建一个新的邮件,然后写下... [email protected] 进入 To: 字段。

有时你会看到这个 "写一封新邮件 "窗口在你发起的应用中以覆盖的方式打开,而苹果邮件应用却没有被打开。

我怎么能达到这个目的呢?

swift xcode email uiapplication
1个回答
2
投票

如果你想从应用程序内部发送电子邮件,你可以看看下面的代码。MFMailComposeViewController

你可以简单地实例化这个视图控制器,将字段设置为subject、cc...,然后呈现出来。

摘自文档。

  1. 检查服务是否可用(例如在模拟器中不可用)
if !MFMailComposeViewController.canSendMail() {
    print("Mail services are not available")
    return
}
  1. 实例化视图控制器,设置委托,并将其呈现出来。
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)
  1. 当邮件发送完毕或用户取消时,解散。
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)
}
© www.soinside.com 2019 - 2024. All rights reserved.