How can I open Facebook and Instagram app by tapping on a button in swift
? Some apps redirect to the Facebook app and open a specific page. How can I do the sam
You actually don't need to use a web and app URL anymore. The web URL will automatically open in the app if the user has it. Instagram or other apps implement this on their end as a Universal Link
Swift 4
func openInstagram(instagramHandle: String) {
guard let url = URL(string: "https://instagram.com/\(instagramHandle)") else { return }
if UIApplication.shared.canOpenURL(url) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
}
Based on accepted answer here is the way to do this more elegantly with Swift 4
UIApplication.tryURL([
"instagram://user?username=johndoe", // App
"https://www.instagram.com/johndoe/" // Website if app fails
])
And truly remember to add the scheme to allow the app to open. However even if you forget that instagram will open in Safari.
The tryUrl is an extension similar to presented here: https://stackoverflow.com/a/29376811/704803