从swift发送电子邮件3

问题描述:

我在尝试使用发送电子邮件选项设置应用程序。从swift发送电子邮件3

我有这样的代码:

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(["[email protected]"]) 
     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中的模拟器设备...所以我认为它应该这样做,但事实并非如此。为什么这不起作用?你能告诉我什么是错的,我该如何前进?

+0

你一个电子邮件地址配置设备?如果不是,那就做吧......它可以解决你的问题。 –

+0

我正在使用模拟器。我如何使用电子邮件地址配置设备?当我去设置我看不到选项“电子邮件”.... –

+0

我的事情,你已经解决了这个问题..同时这里是链接配置iOS设备的电子邮件... https://支持。 apple.com/en-in/HT201320 –

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

+0

是的。我百分之百肯定。我正在研究相同类型的应用程序,并发现这一点。 –

+0

好的谢谢:)但我上面写的代码。可以吗? –

+0

是的。代码看起来很好,应该工作。你没有iPhone吗? –

代码似乎是好的,工作正常,如果应用程序是在真实设备

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

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

要测试,您必须使用设备,设备中的邮件应用程序应配置一些邮件(例如:[email protected])。

希望有所帮助。

与您的代码的问题是,该

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

提出了自己本身;-)

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

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

我是这样做的。它看起来像你跟着documentati非常好,我想我会添加我的变体,以帮助其他人。此外,这更多地更新为当前(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(["[email protected]"]) 
    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) 

} 

关于以下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.

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