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
In swift5, use this
guard let instagram = URL(string: "https://www.instagram.com/yourpagename") else { return }
UIApplication.shared.open(instagram)
First, you have to modify Info.plist
to list instagram
and facebook
with LSApplicationQueriesSchemes
. Simply open Info.plist
as a Source Code, and paste this:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram</string>
<string>fb</string>
</array>
When you want to open the Facebook App and direct to a Facebook-Page, use the Page-ID. Here is a Link, where you could find them: https://www.facebook.com/help/1503421039731588
fb://profile – Open Facebook app to the user’s profile OR pages
fb://friends – Open Facebook app to the friends list
fb://notifications – Open Facebook app to the notifications list (NOTE: there appears to be a bug with this URL. The Notifications page opens. However, it’s not possible to navigate to anywhere else in the Facebook app)
fb://feed – Open Facebook app to the News Feed
fb://events – Open Facebook app to the Events page
fb://requests – Open Facebook app to the Requests list
fb://notes – Open Facebook app to the Notes page
fb://albums – Open Facebook app to Photo Albums list Source: https://stackoverflow.com/a/10416399/8642838
Button(action: {
let url = URL(string: "fb://profile/<PAGE_ID>")!
let application = UIApplication.shared
// Check if the facebook App is installed
if application.canOpenURL(url) {
application.open(url)
} else {
// If Facebook App is not installed, open Safari with Facebook Link
application.open(URL(string: "https://de-de.facebook.com/apple")!)
}
}, label: {
Text("Facebook")
})
Take a look at these links, it can help you:
https://instagram.com/developer/mobile-sharing/iphone-hooks/
http://wiki.akosma.com/IPhone_URL_Schemes
Open a facebook link by native Facebook app on iOS
Otherwise, there is a quick example with Instagram for opening a specific profile (nickname: johndoe) here:
var instagramHooks = "instagram://user?username=johndoe"
var instagramUrl = NSURL(string: instagramHooks)
if UIApplication.sharedApplication().canOpenURL(instagramUrl!) {
UIApplication.sharedApplication().openURL(instagramUrl!)
} else {
//redirect to safari because the user doesn't have Instagram
UIApplication.sharedApplication().openURL(NSURL(string: "http://instagram.com/")!)
}
In swift 3;
First you should add this on your Info.plist
Than you can use this code;
let instagramUrl = URL(string: "instagram://app")
UIApplication.shared.canOpenURL(instagramUrl!)
UIApplication.shared.open(instagramUrl!, options: [:], completionHandler: nil)
In swift 4:
Just change appURL and webURL :
twitter://user?screen_name=\(screenName)
instagram://user?screen_name=\(screenName)
facebook://user?screen_name=\(screenName)
- 'openURL' was deprecated in iOS 10.0:
let screenName = "imrankst1221"
let appURL = NSURL(string: "instagram://user?screen_name=\(screenName)")!
let webURL = NSURL(string: "https://twitter.com/\(screenName)")!
if UIApplication.shared.canOpenURL(appURL as URL) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(appURL as URL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(appURL as URL)
}
} else {
//redirect to safari because the user doesn't have Instagram
if #available(iOS 10.0, *) {
UIApplication.shared.open(webURL as URL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(webURL as URL)
}
}
Update for Swift 4 and iOS 10+
OK, there are two easy steps to achieve this in Swift 3:
First, you have to modify Info.plist
to list instagram
and facebook
with LSApplicationQueriesSchemes
. Simply open Info.plist
as a Source Code, and paste this:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram</string>
<string>fb</string>
</array>
After that, you can open instagram
and facebook
apps by using instagram://
and fb://
. Here is a complete code for instagram and you can do the same for facebook, you can link this code to any button you have as an Action:
@IBAction func InstagramAction() {
let Username = "instagram" // Your Instagram Username here
let appURL = URL(string: "instagram://user?username=\(Username)")!
let application = UIApplication.shared
if application.canOpenURL(appURL) {
application.open(appURL)
} else {
// if Instagram app is not installed, open URL inside Safari
let webURL = URL(string: "https://instagram.com/\(Username)")!
application.open(webURL)
}
}
For facebook
, you can use this code:
let appURL = URL(string: "fb://profile/\(Username)")!