ios - 从共享表 (ios) 设置电子邮件发件人和收件人

标签 ios swift

目标:实现一个包含 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/

相关文章:

使用闭包回调的ios内存泄漏

ios - 当我在搜索栏中键入以检索 Google 图书信息时,我的表格 View 没有重新加载

javascript - Phonegap (Cordova) iOS 推送通知 onNotificationAPN 事件在后台触发

objective-c - 如何异步捕获iOS(cocos2d)中的屏幕截图?

xcode - 在 Swift 中使用 Parse 用户查询

ios - 无法在 watchOS 模拟器上安装应用程序 Xcode9、iOS11、watchOS4 应用程序崩溃

ios - 当弹出窗口的源 View 在 Swift 中更改时,如何更新弹出窗口的布局(位置)?

ios - NSString drawAtPoint : and kCGTextFillClip, 为什么不起作用?

ios - ios mvc模式中的http请求

ios - Xcode 警告 : . .. 是 GNU 扩展