2017-02-16 107 views
2

错误我试图通过我的应用程序发送电子邮件,但我得到了那个叫“请设置邮件帐户,以发送电子邮件”在斯威夫特

“请设置邮件帐户,才能发送错误电子邮件”。

我的码块是下面。

import MessageUI 

@IBAction func emailTapped(_ sender: Any) { 

    let mailComposerVC = MFMailComposeViewController() 
    mailComposerVC.mailComposeDelegate = self 
    mailComposerVC.setToRecipients(["[email protected]"]) 
    mailComposerVC.setSubject("Sending you an in-app e-mail...") 
    mailComposerVC.setMessageBody("Sending e-mail in-app is not so bad!", isHTML: false) 

    if MFMailComposeViewController.canSendMail() { 
     self.present(mailComposerVC, animated: true, completion: {() -> Void in }) 
    } 
} 

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Swift.Error?){ 
    controller.dismiss(animated: true) {() -> Void in } 
} 
+0

你有没有设置要测试的设备上的电子邮件帐户? – rmaddy

+5

为什么你做所有的工作来设置邮件控制器,你做检查,看看是否你实际上可以发送电子邮件前? – rmaddy

回答

2

代码看起来不错(尽管可以改进),您需要在您使用的设备上设置您的电子邮件帐户。现在

import MessageUI 
    @IBAction func emailTapped(_ sender: Any) { 

      if MFMailComposeViewController.canSendMail() { 
       let mailComposerVC = MFMailComposeViewController() 
       mailComposerVC.mailComposeDelegate = self 
       mailComposerVC.setToRecipients(["[email protected]"]) 
       mailComposerVC.setSubject("Sending you an in-app e-mail...") 
       mailComposerVC.setMessageBody("Sending e-mail in-app is not so bad!", isHTML: false) 

       self.present(mailComposerVC, animated: true, completion: {() -> Void in }) 
      } 

     } 

     func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Swift.Error?){ 
      controller.dismiss(animated: true) {() -> Void in } 

     } 

,搜索Settings应用程序在你的iPhone,然后转到Mail - >Accounts - >Add Account设置电子邮件在手机上的帐户。 你可以看看这个video

+0

ystack嘿,我找不到配置部分,也是你的解决方案在Xcode。您能否详细说明一下,谢谢 – gozdebal

+3

邮件帐户的设置在您的iOS设备上,而不是在Xcode上。该设置仅在真实设备上才可用于模拟器。 – rckoenes

+0

好的,rckoenes非常感谢! – gozdebal

相关问题