I'm trying to do what the title says but when i tap the button to open mail my app crashes when i don't have mail app installed. can you help me sort this out?
Code:
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")!)
}
}
Crash gathered logs :
libc++abi.dylib: terminating with uncaught exception of type NSException
crashes on line : class AppDelegate: UIResponder, UIApplicationDelegate{
. Extra info : Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target