I'm trying to open URL after clicking on alert's button. I've tried different approaches, but none of it worked. Here's my code:
let alert = UIAlertController(title: "Warning", message: "Do you want to open the link?", preferredStyle: .Alert);
alert.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: nil));
let okAction = UIAlertAction(title: "Open in Safari", style: .Default) {(action) in
UIApplication.sharedApplication().openURL(NSURL(string: "www.google.com")!);
return;
});
alert.addAction(okAction);
self.presentViewController(alert, animated: true, completion: nil);
If I put some simple function like println
, it works fine.
For Swift 3 Users, this is how to have a button that open a link in an alert: