【问题标题】:Open mail screen in own app instead of mail app在自己的应用程序而不是邮件应用程序中打开邮件屏幕
【发布时间】:2020-06-11 19:46:31
【问题描述】:

我使用的是最新的XcodeSwift 版本。

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

UIApplication.shared.open(URL(string: "mailto:test@example.com")!)

此代码打开 Apple 邮件应用程序,创建新电子邮件并将 test@example.com 写入 To: 字段。

有时您会看到这个“写一封新电子邮件”窗口在您启动它的应用程序中以覆盖形式打开,而 Apple 邮件应用程序没有打开。

我怎样才能做到这一点?

【问题讨论】:

    标签: swift xcode email uiapplication


    【解决方案1】:

    如果您想从应用程序中发送电子邮件,可以查看MFMailComposeViewController

    您可以简单地实例化此视图控制器,将字段设置为主题,抄送...并呈现它。

    取自文档:

    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(["address@example.com"])
    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)
    }
    

    【讨论】:

      猜你喜欢
      • 2012-05-17
      • 1970-01-01
      • 2019-06-29
      • 2015-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      相关资源
      最近更新 更多