通过swift 3发送电子邮件

时间:2022-10-23 19:23:47

I am trying to set up an app with send email option.

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

I have this code:

我有这段代码:

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.
    }
}

So I get this message: "Mail services are not available". Now I've logged in the simulator device in iCloud... So i think it should do it but it's not. Why isn't this working? Can you tell me whats wrong and how can I move forward?

所以我得到了这样的信息:“邮件服务不可用”。现在我已经登录了iCloud的模拟器设备…所以我认为它应该这么做,但它不是。为什么这不是工作吗?你能告诉我哪里出了问题,我该如何前进吗?

4 个解决方案

#1


36  

It will not work with simulator. Please test it on iPhone device. You can refer Apple Developer Portal - MFMailComposeViewController

它不会使用模拟器。请在iPhone设备上测试。您可以参考Apple Developer Portal - MFMailComposeViewController

#2


19  

Here's how I did it. It looks like you followed the documentation very well, I thought I'd add my variation in case it helps someone else. Plus, this is a little more updated to current (Aug 2017) syntax.

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

Conform to the MFMailComposeViewControllerDelegate protocol, and check if the device can send mail.

遵守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
    }
}

My app uses an IBAction to initiate the mail composition.

我的应用程序使用IBAction来启动邮件组合。

@IBAction func sendFeedbackButtonTapped(_ sender: Any) {

    let composeVC = MFMailComposeViewController()
    composeVC.mailComposeDelegate = self

    // Configure the fields of the interface.
    composeVC.setToRecipients(["ctr89josh@gmail.com"])
    composeVC.setSubject("StoryBook Feedback")
    composeVC.setMessageBody("Hey Josh! Here's my feedback.", isHTML: false)

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

}

About the following mailComposeController function, the documentation says

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

The mail compose view controller is not dismissed automatically. When the user taps the buttons to send the email or cancel the interface, the mail compose view controller calls the mailComposeController(_:didFinishWith:error:) method of its delegate. Your implementation of that method must dismiss the view controller explicitly, as shown in Listing 3. You can also use this method to check the result of the operation.

邮件组合视图控制器不会自动取消。当用户点击按钮发送电子邮件或取消界面时,邮件撰写视图控制器调用其委托的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)
   }
}

Source Apple Documentation: MFMailComposeViewController

源文档:苹果MFMailComposeViewController

#3


4  

Code seems to be good and works fine if the app is running in a real device

如果应用程序运行在一个真实的设备中,代码看起来很好,运行良好。

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

You can't test it on simulator,You'll be able to test basic things like UI,How the things are happening on Cancel/Send button clicks.

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

To test,You have to use a device,The Mail application in the device should be configured with some mail(ex: abc@xyz.com).

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

Hope that helps.

希望有帮助。

#4


0  

The problem with your code is, that the

您的代码的问题是

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

//模态显示视图控制器。自我。present(composeVC, animated: true, completion: nil)

presents itself in itself ;-)

自我表现;

If you don´t know the current view controller, just display it on the RootViewController, i.e.

´如果你不知道当前视图控制器,显示它RootViewController,即。

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

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

#1


36  

It will not work with simulator. Please test it on iPhone device. You can refer Apple Developer Portal - MFMailComposeViewController

它不会使用模拟器。请在iPhone设备上测试。您可以参考Apple Developer Portal - MFMailComposeViewController

#2


19  

Here's how I did it. It looks like you followed the documentation very well, I thought I'd add my variation in case it helps someone else. Plus, this is a little more updated to current (Aug 2017) syntax.

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

Conform to the MFMailComposeViewControllerDelegate protocol, and check if the device can send mail.

遵守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
    }
}

My app uses an IBAction to initiate the mail composition.

我的应用程序使用IBAction来启动邮件组合。

@IBAction func sendFeedbackButtonTapped(_ sender: Any) {

    let composeVC = MFMailComposeViewController()
    composeVC.mailComposeDelegate = self

    // Configure the fields of the interface.
    composeVC.setToRecipients(["ctr89josh@gmail.com"])
    composeVC.setSubject("StoryBook Feedback")
    composeVC.setMessageBody("Hey Josh! Here's my feedback.", isHTML: false)

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

}

About the following mailComposeController function, the documentation says

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

The mail compose view controller is not dismissed automatically. When the user taps the buttons to send the email or cancel the interface, the mail compose view controller calls the mailComposeController(_:didFinishWith:error:) method of its delegate. Your implementation of that method must dismiss the view controller explicitly, as shown in Listing 3. You can also use this method to check the result of the operation.

邮件组合视图控制器不会自动取消。当用户点击按钮发送电子邮件或取消界面时,邮件撰写视图控制器调用其委托的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)
   }
}

Source Apple Documentation: MFMailComposeViewController

源文档:苹果MFMailComposeViewController

#3


4  

Code seems to be good and works fine if the app is running in a real device

如果应用程序运行在一个真实的设备中,代码看起来很好,运行良好。

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

You can't test it on simulator,You'll be able to test basic things like UI,How the things are happening on Cancel/Send button clicks.

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

To test,You have to use a device,The Mail application in the device should be configured with some mail(ex: abc@xyz.com).

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

Hope that helps.

希望有帮助。

#4


0  

The problem with your code is, that the

您的代码的问题是

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

//模态显示视图控制器。自我。present(composeVC, animated: true, completion: nil)

presents itself in itself ;-)

自我表现;

If you don´t know the current view controller, just display it on the RootViewController, i.e.

´如果你不知道当前视图控制器,显示它RootViewController,即。

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

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