代码之家  ›  专栏  ›  技术社区  ›  RandomGeek

如果安装了邮件,则执行功能,如果未打开应用商店页面

  •  2
  • RandomGeek  · 技术社区  · 7 年前

    我正试着按照标题所说的做,但当我点击按钮打开邮件时,我的应用程序在没有安装邮件应用程序的情况下崩溃了。你能帮我解决这个问题吗?

    代码:

    if UIApplication.shared.canOpenURL(URL(string: "mailto://")!) {
                // Mail is installed. Launch Mail and start function:
                let toRecipients = ["oExample@gmail.com"]
    
            let mc: MFMailComposeViewController = MFMailComposeViewController()
    
            mc.mailComposeDelegate = self
    
            mc.setToRecipients(toRecipients)
            mc.setSubject("Why does it crash?")
    
            mc.setMessageBody("שם הבר: \(CrashNameField.text!) \n\nעיר: \(CrashReasonNameField.text!)", isHTML: false)
    
            self.present(mc, animated: true, completion: nil)
    
        }else {
                // Mail is not installed. Launch AppStore to install Mail app
                UIApplication.shared.openURL(URL(string: "https://itunes.apple.com/gb/app/mail/id1108187098?mt=8")!)
            }
        }
    

    libc++abi.dylib: terminating with uncaught exception of type NSException 在线崩溃: class AppDelegate: UIResponder, UIApplicationDelegate{ .额外信息: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target

    1 回复  |  直到 7 年前
        1
  •  2
  •   Nazmul Hasan    7 年前

    试着这样做

     var picker = MFMailComposeViewController()
        if MFMailComposeViewController.canSendMail() {
            picker.mailComposeDelegate = self
            picker.setSubject("Test mail")
            picker.setMessageBody(messageBody.text, isHTML: true)
            present(picker as? UIViewController ?? UIViewController(), animated: true) { _ in }
        }
    

    original post here