Orionss Orionss - 2 months ago 28
Swift Question

How to add a hyperlink button on iOS Swift?

I'm trying to make a clickable link over a button.
I made this using some Internet help but it didn't work :

@IBAction func linkClicked(sender: AnyObject) {
openUrl("http://fr.envisite.net/t5exce")
}
func openUrl(url:String!) {

let targetURL=NSURL(fileURLWithPath: url)

let application=UIApplication.sharedApplication()

application.openURL(targetURL);

}


It doesn't do anything, no error, just the button doesn't get me on Safari (I use the iOS simulator)

Answer

You're creating a file URL with a web url string. Use the NSURL String constructor instead.

@IBAction func linkClicked(sender: AnyObject) {
    openUrl("http://fr.envisite.net/t5exce")
}

func openUrl(urlStr:String!) {

     if let url = NSURL(string:urlStr) {
UIApplication.sharedApplication().openURL(url)
}

   }
Comments