I was trying to open mail on UILabel click event. But its throws fatal error:
unexpectedly found nil while unwrapping an Optional value.
Code used:
func sendMail(sender:UITapGestureRecognizer){
print("mail::" + self.lblMail.text!) // [email protected] is here
let url = NSURL(string: "mailto:\(self.lblMail.text)")! //url is nil when debugged
UIApplication.sharedApplication().openURL(url)
}
Check to make sure self.lblMail.text is non-nil before proceeding by unwrapping it with an if let:
if let email = self.lblMail.text {
let url = NSURL(string: "mailto:\(email)")!
UIApplication.sharedApplication().openURL(url)
}
If you get the error:
LaunchServices: ERROR: There is no registered handler for URL scheme mailto
Make sure you're running this code on an actual device and not the iOS Simulator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With