39

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 same thing?

I found it:

var url = NSURL(string: "itms://itunes.apple.com/de/app/x-gift/id839686104?mt=8&uo=4")

if UIApplication.sharedApplication().canOpenURL(url!) {
  UIApplication.sharedApplication().openURL(url!)
}

but I have to know the app URL. Other examples were in ObjectiveC, which I don't know =/

budiDino
  • 13,044
  • 8
  • 95
  • 91

9 Answers9

46

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)")!
budiDino
  • 13,044
  • 8
  • 95
  • 91
Ibrahim
  • 6,006
  • 3
  • 39
  • 50
40

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/")!)
}
budiDino
  • 13,044
  • 8
  • 95
  • 91
jregnauld
  • 1,288
  • 18
  • 17
  • but what about youtube? How can I use it? `youtube://channel/channel_code`? I did it and it doesn't show me my channel –  Apr 28 '15 at 06:23
  • 1
    As the answer should've pointed to you, when you have such a question you need to look for API of the youtube app (or any other app you are interested in). Alternatively you can download the app and check its info.plist to research available app url schemes, but only if there's no documentation as this might not be supported across all versions. – A-Live Apr 28 '15 at 11:35
  • @A-Live I looked at API Documentation, but nothing here about iPhone hooks –  Apr 28 '15 at 11:48
  • Swift 4 version: if UIApplication.shared.canOpenURL(URL(string:yourUrl)!) { UIApplication.shared.open( URL(string: yourUrl)!, options: [:]) } else { //redirect to safari because the user doesn't have Instagram UIApplication.shared.open( URL(string: yourUrl)!, options: [:]) } – Carioni May 29 '18 at 12:32
  • The top two links you provided are dead. – Lemon Dec 10 '20 at 10:12
12

In swift5, use this

   guard let instagram = URL(string: "https://www.instagram.com/yourpagename") else { return }
   UIApplication.shared.open(instagram)
Talha Rasool
  • 1,126
  • 14
  • 12
9

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)
        }
    }
}
DoesData
  • 6,594
  • 3
  • 39
  • 62
8

In swift 3;

First you should add this on your Info.plist

enter image description here

Than you can use this code;

    let instagramUrl = URL(string: "instagram://app")
    UIApplication.shared.canOpenURL(instagramUrl!)
    UIApplication.shared.open(instagramUrl!, options: [:], completionHandler: nil)
Celil Bozkurt
  • 1,693
  • 16
  • 18
7

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)
        }
    }
Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60
6

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

Pomo-J
  • 132
  • 2
  • 6
  • `tryURL` is not a Swift 4 feature... it's an extension you added (probably similar to this one: https://stackoverflow.com/a/29376811/704803) – andrewtweber Apr 07 '18 at 09:08
  • @andrewtweber you are right! How could I have missed that :( Added your suggestion to the answer itself. – Pomo-J Apr 16 '18 at 17:13
5

SwiftUI Version

Add in Info.plist

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

Schemes

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

SwiftUI-Code Version

    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")
    })
Jonas Deichelmann
  • 3,513
  • 1
  • 30
  • 45
0

For opening instagram or facebook pages from your app, It worked for me just to use links like www.facebook.com/user , or www.instagram.com/user

The instagram and facebook apps opened automatically when doing this.

  • This depend on device settings, it could also open the browser, in your case, your device already remember that you want to view those sites in their respective apps. – phoenixstudio Jan 30 '21 at 12:36