ios - 使用 Apple Mail 应用程序以外的外部应用程序发送电子邮件

标签 ios objective-c email mfmailcomposeviewcontroller mfmailcomposer

有没有什么方法可以发送电子邮件并让设备上所有可以这样做的应用程序处理它?例如 GMail、Yahoo、Outlook,还是需要使用各自的库来实现这样的功能?

是否可以使用某种通用 URL 或方案来提供设备上所有可用电子邮件客户端的选择?

目前,为了撰写电子邮件,我使用 MFMailComposeViewController,但如果用户没有使用 Mail 应用程序设置帐户(许多人没有),它就无法工作。

最佳答案

几个月前我遇到了完全相同的问题(尤其是在从模拟器进行测试时,因为它没有设置邮件帐户,因此发生了崩溃)。您需要在 MFMailComposeViewControllerDelegate

中验证此流程
let recipient = "whoever@youwant.com"
if MFMailComposeViewController.canSendMail() {
    // Do your thing with native mail support
} else { // Otherwise, 3rd party to the rescue
    guard let urlEMail = URL(string: "mailto:\(recipient)") else { 
        print("Invalid URL Scheme")
        return 
    }
    if UIApplication.shared.canOpenURL(urlEMail) {
        UIApplication.shared.open(urlEMail, options: [:], completionHandler: {
            _ in
        })
    } else {
        print("Ups, no way for an email to be sent was found.")
    }
}

上面有很多过度验证,但那是出于调试原因。如果您完全确定该电子邮件地址(例如,之前的正则表达式匹配),那么一定要强制解包;否则,这将确保您的代码安全。

希望对您有所帮助!

关于ios - 使用 Apple Mail 应用程序以外的外部应用程序发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48284353/

相关文章:

ios - 收据验证 - 删除应用程序后是否保留收据?

objective-c - objective-c ,动画 block ,动画和完成之间的延迟

objective-c - NSTimer 倒计时标签显示剩余时间

iphone - '+[NSInspiration调用WithMethodSignature :]: method signature argument cannot be nil' (Cocos2D)

email - 电子邮件地址中的+在哪里定义的?

ruby-on-rails - Gmail未检测到回复字段

linux - 在 Redhat 中如何从文件中提取文本并将其作为正文通过电子邮件发送?

ios - 当我触摸标签栏项目时,我没有收到任何通知

ios - 在 Realm - Swift 中按字符串值(语言环境)对数组进行排序

ios - 不同的纵向和横向自动布局约束 : where to set them when the device is firSTLy in landscape?