【问题标题】:Sending an email from swift 3从 swift 3 发送电子邮件
【发布时间】:2016-11-30 11:54:40
【问题描述】:

我正在尝试设置一个带有发送电子邮件选项的应用程序。

我有这个代码:

import Foundation
import MessageUI
import UIKit

class emailClass: UIViewController, MFMailComposeViewControllerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()

        if !MFMailComposeViewController.canSendMail() {
            print("Mail services are not available")
            return
        }        
        sendEmail() 
    }

    func sendEmail() {      
        let composeVC = MFMailComposeViewController()
        composeVC.mailComposeDelegate = self
        // Configure the fields of the interface.
        composeVC.setToRecipients(["address@example.com"])
        composeVC.setSubject("Hello!")
        composeVC.setMessageBody("Hello this is my message body!", 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)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

所以我收到这条消息:“邮件服务不可用”。 现在我已经在 iCloud 中登录了模拟器设备......所以我认为它应该这样做,但事实并非如此。为什么这不起作用?你能告诉我哪里出了问题,我该如何继续前进?

【问题讨论】:

  • 您是否为您的设备配置了电子邮件地址?如果没有,那就去做吧……它可能会解决你的问题。
  • 我正在使用模拟器。如何使用电子邮件地址配置设备?当我进入设置时,我看不到“电子邮件”选项......
  • 我的东西,你已经解决了这个问题.. 同时这里是为 iOS 设备配置电子邮件的链接...... support.apple.com/en-in/HT201320
  • 100% 工作和测试在这里,stackoverflow.com/questions/36838166/…

标签: ios swift iphone swift3 messageui


【解决方案1】:

它不适用于模拟器。请在 iPhone 设备上进行测试。可以参考Apple Developer Portal - MFMailComposeViewController

【讨论】:

  • 是的。我百分百确定。我正在开发相同类型的应用程序并找到了这个。
  • 好的,谢谢 :) 但是我上面写的代码。没事吧?
  • 是的。代码看起来不错,应该可以工作。你没有 iPhone 吗?
【解决方案2】:

这就是我的做法。看起来您很好地遵循了文档,我想我会添加我的变体以防它帮助其他人。另外,这是对当前(2017 年 8 月)语法的更新。

符合MFMailComposeViewControllerDelegate协议,检查设备是否可以发送邮件。

import Foundation
import UIKit
import MessageUI

class WelcomeViewController: UIViewController, MFMailComposeViewControllerDelegate {


override func viewDidLoad() {
    super.viewDidLoad()

    if !MFMailComposeViewController.canSendMail() {
        print("Mail services are not available")
        return
    }
}

我的应用程序使用 IBAction 来启动邮件撰写。

@IBAction func sendFeedbackButtonTapped(_ sender: Any) {

    let composeVC = MFMailComposeViewController()
    composeVC.mailComposeDelegate = self

    // Configure the fields of the interface.
    composeVC.setToRecipients(["exampleEmail@email.com"])
    composeVC.setSubject("Message Subject")
    composeVC.setMessageBody("Message content.", isHTML: false)

    // Present the view controller modally.
    self.present(composeVC, animated: true, completion: nil)

}

关于下面的mailComposeController函数,文档说

邮件撰写视图控制器不会自动关闭。当用户点击按钮发送邮件或取消界面时,邮件撰写视图控制器调用 mailComposeController(_:didFinishWith:error:) 其委托的方法。该方法的实现必须显式关闭视图控制器,如清单 3 所示。您还可以使用该方法检查操作结果。

func mailComposeController(_ controller: MFMailComposeViewController,
                           didFinishWith result: MFMailComposeResult, error: Error?) {
    // Check the result or perform other tasks.

    // Dismiss the mail compose view controller.
    controller.dismiss(animated: true, completion: nil)
   }
}

来源Apple Documentation: MFMailComposeViewController

【讨论】:

  • 强调一些会破坏事物的东西:在旧版本中,...didFinishWith result:... 标头中的error: 的类型是NSError?。在新版本中,它只是Error?。如果您没有与预期相同的 EXACT 标头,则您的委托将无法正常工作——关闭视图控制器的代码部分将无法运行,并且邮件将保留在您的其他视图之上。我花了很长时间才弄清楚这种细微的差别。希望这可以帮助某人。
【解决方案3】:

Swift 5 中发送电子邮件很容易,您需要确认并实现 MFMailComposeViewControllerDelegate 并检查我们是否可以在此设备上发送电子邮件

这是我用于我的任务的一小段代码

import UIKit
import MessageUI

class ViewController: UIViewController, MFMailComposeViewControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    //MARK: IBAction Method for Button click
    @IBAction func sendEmail(_ sender: Any) {
        //TODO:  You should chack if we can send email or not
        if MFMailComposeViewController.canSendMail() {
            let mail = MFMailComposeViewController()
            mail.mailComposeDelegate = self
            mail.setToRecipients(["you@yoursite.com"])
            mail.setSubject("Email Subject Here")
            mail.setMessageBody("<p>You're so awesome!</p>", isHTML: true)
            present(mail, animated: true)
        } else {
            print("Application is not able to send an email")
        }
    }

    //MARK: MFMail Compose ViewController Delegate method
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true)
    }
}

PS:请不要忘记您需要在真实设备上进行测试

【讨论】:

    【解决方案4】:

    如果应用程序在真实设备上运行,代码似乎很好并且运行良好

    MFMailComposeViewController.canSendMail() // returns false for simulators.
    

    你不能在模拟器上测试它,你可以测试基本的东西,比如 UI,点击取消/发送按钮时事情是如何发生的。

    要测试,你必须使用设备,设备中的邮件应用程序应该配置一些邮件(例如:abc@xyz.com)。

    希望对您有所帮助。

    【讨论】:

      【解决方案5】:

      你的代码的问题是,

      // 模态显示视图控制器。 self.present(composeVC, 动画: true, 完成: nil)

      自我呈现;-)

      如果您不知道当前的视图控制器,只需将其显示在 RootViewController 上,即

      UIApplication.shared.keyWindow?.rootViewController?.present(...

      【讨论】:

        猜你喜欢
        • 2011-06-28
        • 2018-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-26
        • 2011-03-07
        • 2011-01-30
        • 2017-12-23
        相关资源
        最近更新 更多