odonckers odonckers - 5 months ago 19x
Swift Question

How can I launch Safari from an iPhone app in Swift 2?

I know that you can do this in Objective-C, but I have yet to do this in Swift 2. Does anyone know how to do this in Swift 2? Here is the link to to the Objective-C solution. Here.
I have tried this.


Every time I run it crashes the app. Please help me, I am stumped.


Sorry that it took me so long to write a follow up answer. The past year has been hectic for me. But I have figured out the best way to do this is bellow:

UIApplication.sharedApplication().openURL(NSURL(string: "google.com")!)

But... If any of you are looking to figure out what the error is, please look at dede.exe's answer, it has been marked as the correct answer.


Maybe your code is not protected enough.

Try this snippet:

//Check if is a valid URL
guard let url = NSURL(string: "https://www.google.com.br") else
    print("Error to upen url")

//Check if can open URL
guard UIApplication.sharedApplication().canOpenURL(url) else
    print("Can't open URL")

//If reach this point... You have a valid URL and your app can open it.   

//If you still have a BAD_EXEC over here... believe... it´s some kind of witchcraft :O

In complement, if you have some error with non https url... Look at this link: NSData contentsOfUrl returns nil