如果安装了 Mail 则执行功能,如果没有则打开应用商店页面
If Mail is installed Do function , if not Open app store page
我正在尝试按照标题所说的操作,但是当我点击按钮打开邮件时,我的应用程序崩溃了,而我没有安装邮件应用程序。你能帮我解决这个问题吗?
代码:
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
试试这个方法
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
我正在尝试按照标题所说的操作,但是当我点击按钮打开邮件时,我的应用程序崩溃了,而我没有安装邮件应用程序。你能帮我解决这个问题吗?
代码:
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
试试这个方法
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