【问题标题】:How to use MFMailComposeViewController with simulator?如何在模拟器中使用 MFMailComposeViewController?
【发布时间】:2015-10-04 20:59:36
【问题描述】:

我想在 Swift 2.0 和 Xcode 7.0.1 中使用 MFMailComposeViewController()。我担心,这是一个重复的问题,但没有找到解决方案。

我的代码:

@IBAction func sendMail(sender: AnyObject) {
  let picker = MFMailComposeViewController()
  picker.mailComposeDelegate = self
  picker.setSubject(subject.text!)
  picker.setMessageBody(body.text, isHTML: true)
  presentViewController(picker, animated: true, completion: nil)
}

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
   switch result.rawValue {
   case MFMailComposeResultCancelled.rawValue:
       print("Mail cancelled")
   case MFMailComposeResultSaved.rawValue:
       print("Mail saved")
  case MFMailComposeResultSent.rawValue:
       print("Mail sent")
  case MFMailComposeResultFailed.rawValue:
       print("Mail sent failure: \(error!.localizedDescription)")
  default:
      break
 }
 dismissViewControllerAnimated(true, completion: nil)

错误被抛出 print("邮件已取消")

当我在我的设备 (iPad) 上进行测试时,一切都很好。但是当我使用模拟器时,我得到了错误

 viewServiceDidTerminateWithError: Error          
 Domain=_UIViewServiceInterfaceErrorDomain Code=3 "(null)" 
 UserInfo={Message=Service Connection Interrupted}
 Mail cancelled

但我想在模拟器中运行它以查看它的外观,即在 iPhone 6 上...

【问题讨论】:

  • 您是否在模拟器上设置了电子邮件帐户?
  • @Fogmeister: upps: 我该怎么做?
  • 与您的 iPhone 上的相同。只需进入模拟器上的设置并正常设置电子邮件即可。
  • 好吧,以为我疯了,但在设置中找不到... :-(
  • 尝试在模拟器上打开邮件。我没有我的电脑 tam,所以我可以检查一下。但它在实际模拟器的设置中。像使用手机一样使用它。不是正常的 Mac 偏好设置。

标签: ios-simulator swift2 mfmailcomposeviewcontroller


【解决方案1】:

您必须先检查设备是否可以使用MFMailComposeViewController.canSendMail()发送邮件

@IBAction func sendMail(sender: AnyObject) {
    guard MFMailComposeViewController.canSendMail()
        else { 
            // TODO: Alert the user its device cannot send mail
            print("Mail services are not available")
            return 
    } 

    let picker = MFMailComposeViewController()
    picker.mailComposeDelegate = self
    picker.setSubject(subject.text!)
    picker.setMessageBody(body.text, isHTML: true)
    presentViewController(picker, animated: true, completion: nil)
}

【讨论】:

    猜你喜欢
    • 2011-08-21
    • 1970-01-01
    • 2020-02-27
    • 2011-02-13
    • 2021-02-11
    • 1970-01-01
    • 2020-03-07
    • 2020-01-19
    • 1970-01-01
    相关资源
    最近更新 更多