目标:实现一个包含 Gmail 作为选项的共享表 (UIActivityViewController),并设置收件人和主题
问题:设置主题有效,但设置收件人(forKey:“to”)会使程序崩溃。
我希望能够设置共享表电子邮件客户端的发件人和收件人字段,但到目前为止我一直找不到任何方法可以做到这一点。感谢您的帮助!
相关代码:
let email = ["bob@bobsCompany.com"]
let activityVC = UIActivityViewController(activityItems: [""], applicationActivities: nil)
activityVC.excludedActivityTypes = excludedActivity
activityVC.setValue("Subject Title", forKey: "subject")
activityVC.setValue(email[0], forKey: "to")
activityVC.popoverPresentationController?.sourceView = self.view
self.presentViewController(activityVC, animated: true, completion: nil)
最佳答案
您只能为 UIActivityViewController 设置 Subject。程序崩溃是因为没有“to”键。如果要设置收件人和发件人字段,则必须使用 MailMFComposeViewController。
if !MFMailComposeViewController.canSendMail()
{
let alertMsg = UIAlertController(title: "Test", message: "Mail services not available.", preferredStyle: UIAlertControllerStyle.Alert)
alertMsg.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alertMsg, animated: true, completion: nil)
}
else
{
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
// Configure the fields of the interface.
composeVC.setToRecipients(["mail@egmail.com"])
composeVC.setSubject("Subject)
composeVC.setMessageBody("Hi.", isHTML: false)
// Present the view controller modally.
self.presentViewController(composeVC, animated: true, completion: nil)
}
关于ios - 从共享表 (ios) 设置电子邮件发件人和收件人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38984893/