Xcode swift link to facebook page
The problem is with the format of your Facebook url so notice the format. I use this extension to open urls. You provide it with an array of urls in the order you want them to try to be opened and it tries the first one first and if it fails it goes to the second one and so on:
extension UIApplication {
class func tryURL(urls: [String]) {
let application = UIApplication.sharedApplication()
for url in urls {
if application.canOpenURL(NSURL(string: url)!) {
application.openURL(NSURL(string: url)!)
return
}
}
}
}
And for use:
UIApplication.tryURL([
"fb://profile/116374146706", // App
"http://www.facebook.com/116374146706" // Website if app fails
])
[Update] for Swift 4:
extension UIApplication {
class func tryURL(urls: [String]) {
let application = UIApplication.shared
for url in urls {
if application.canOpenURL(URL(string: url)!) {
application.openURL(URL(string: url)!)
return
}
}
}
}
And then:
UIApplication.tryURL(urls: [
"fb://profile/116374146706", // App
"http://www.facebook.com/116374146706" // Website if app fails
])
[Update] for iOS 10 / Swift 5
extension UIApplication {
class func tryURL(urls: [String]) {
let application = UIApplication.shared
for url in urls {
if application.canOpenURL(URL(string: url)!) {
if #available(iOS 10.0, *) {
application.open(URL(string: url)!, options: [:], completionHandler: nil)
}
else {
application.openURL(URL(string: url)!)
}
return
}
}
}
}
Swift 3
extension UIApplication {
class func tryURL(urls: [String]) {
let application = UIApplication.shared
for url in urls {
if application.canOpenURL(URL(string: url)!) {
//application.openURL(URL(string: url)!)
application.open(URL(string: url)!, options: [:], completionHandler: nil)
return
}
}
}
}
And for use:
UIApplication.tryURL(urls: [
"fb://profile/116374146706", // App
"http://www.facebook.com/116374146706" // Website if app fails
])