我正在try 设置一个带有发送邮箱选项的应用程序.

我有这个密码:

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

所以我收到了这样一条信息:"邮件服务不可用".

推荐答案

它不能与模拟器一起工作.请在iPhone设备上测试.你可以参考Apple Developer Portal - MFMailComposeViewController

Swift相关问答推荐

通过withstickedContinuation传递通用类型T

SwiftData:线程1:&Quot;NSFetchRequest找不到实体名称';提醒&q;的NSEntityDescription

如何将新事例添加到枚举中?

Swift String.firstIndex(of: "\n") 返回 nil,即使字符串有 \n

在 init(from decoder: Decoder) 方法中访问原始 JSON 数据

在 SwiftUI 中禁用 Select 器选项

如何在 swift 5.0 中获取当前行

Swift Random Float Fatal Error:在无限范围内没有均匀分布

swift 是否遇到 Java 的 2gb 最大序列化大小问题?

如何制作具有 2 个视图的 3D 旋转动画?

RxSwift 事件触发了两次

令人满意的 var body:协议扩展中的一些视图

Swift中的分段错误

如果没有标记,则为 Swift 预处理器

使 struct 可散列?

Swift 自定义字体 Xcode

Swift 3:小数到 Int

Swift 中的可选数组与空数组

如何使用 Swift 将文本文件逐行加载到数组中?

如何将多个枚举值作为函数参数传递