【问题标题】:Sending an email from an app using MFMail使用 MFMail 从应用程序发送电子邮件
【发布时间】:2016-02-04 22:56:13
【问题描述】:

我正在尝试让我的应用能够在按下按钮后访问和发送电子邮件。这是我到目前为止所拥有的,当我运行它时,单击帮助按钮会弹出警报并且取消按钮工作正常,但警报的电子邮件部分会使应用程序崩溃。 当它崩溃时,它会突出显示class AppDelegate: UIResponder, UIApplicationDelegate 行并显示Thread 1: signal SIGABRT

@IBAction func helpButtonAction(sender: UIButton) {
        let alert = UIAlertController(title: "Help", message: "Click 'Email' to email support", preferredStyle: UIAlertControllerStyle.Alert)

    alert.addAction(UIAlertAction(title: "Email", style: UIAlertActionStyle.Default, handler: { action in
        let emailTitle = "Help Request"
        let messageBody = ""
        let toRecipents = ["sample@sample.com"]
        let mc: MFMailComposeViewController = MFMailComposeViewController()
        mc.mailComposeDelegate = self
        mc.setSubject(emailTitle)
        mc.setMessageBody(messageBody, isHTML: false)
        mc.setToRecipients(toRecipents)

        self.presentViewController(mc, animated: true, completion: nil)
    }))

    alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)
}

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
    self.dismissViewControllerAnimated(false, completion: nil)
}

这是控制台错误...

    'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <app.ViewController: 0x13c64a5f0>.'
*** First throw call stack:
(0x183930f48 0x19847ff80 0x18923def4 0x189240800 0x188fbdea0 0x10004909c 0x1000491d0 0x100048cbc 0x100048d0c 0x1893297d8 0x189329f70 0x189218ba4 0x18921bd9c 0x188ff3668 0x188eff240 0x188efed3c 0x188efebc4 0x1886c5c2c 0x1013d5c68 0x1013db710 0x1838e81f8 0x1838e6060 0x183814ca0 0x18e87c088 0x188f2cffc 0x10004ee78 0x198cc28b8)
libc++abi.dylib: terminating with uncaught exception of type NSException

【问题讨论】:

    标签: swift xcode7 mfmailcomposeviewcontroller


    【解决方案1】:

    问题在于您如何实例化视图控制器。

    此代码将实例化类,但不会实例化您想要的视图。

    let mc: MFMailComposeViewController = MFMailComposeViewController()
    

    这样做的正确方法,可以在上面的链接中找到,我将在这里复制代码。

    您必须为您的视图控制器设置一个标识符,通过情节提要对其进行实例化,然后将其呈现。

    let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
    self.presentViewController(vc, animated: true, completion: nil)
    

    Instantiate and Present a viewController in Swift

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-20
      • 2021-06-08
      • 2020-05-24
      • 2011-05-08
      • 2011-08-06
      • 2011-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多