69

I want to redirect user to my App's Facebook page, so I have the following code:

[[UIApplication sharedApplication] openURL:
    [NSURL URLWithString: @"https://facebook.com/myAppsPage"]];

this works great, if there is no Facebook app on device. Then the link opens in Safari.

If there is Facebook app installed on device, then after executing above code Facebook app opens, but it is showing users timeline, not my app's page. I tried to change link to @"fb://pages/myAppsPage" and to @"fb://profile/myAppsPage, but this again opened users timeline.

So, my question is: how can I open my apps page in safari, if there is no Facebook app, or in Facebook app, if the one is installed.

medvedNick
  • 4,512
  • 4
  • 32
  • 50

8 Answers8

124

You have to use the facebook ID for the given page rather than the vanity URL. To get the pages ID, enter your page's vanity name at the following URL and copy the value for "id":

https://graph.facebook.com/yourappspage

Once you have the id, you can set up the method to check if FB is installed using the canOpenURL method and serve the appropriate content for both states:

NSURL *facebookURL = [NSURL URLWithString:@"fb://profile/113810631976867"];
if ([[UIApplication sharedApplication] canOpenURL:facebookURL]) {
    [[UIApplication sharedApplication] openURL:facebookURL];
} else {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://facebook.com"]];
}
Sean Kladek
  • 4,396
  • 1
  • 23
  • 29
  • 1
    No problem. Just be aware, URL schemes are almost completely undocumented and it's up to Facebook to continue to support this feature. They could make breaking changes to it at any given time. – Sean Kladek May 16 '13 at 15:33
  • 3
    Thanks for the great tip of using `fb://profile/xxxxx` that works well in current version of Facebook app. Had been searching over the internet for a solution, the suggestion of using of `fb://page/xxxxx` which does **not** work, no page is loaded, probably that's for older version of Facebook app. – P.L. Aug 06 '13 at 03:05
  • 1
    Confirmed that this is still working successfully on iOS7 and iOS8. – Derek Lee Feb 23 '15 at 02:42
  • 7
    This doesn't work anymore with the deprecation of IDs by Facebook. – kopiro Jun 29 '15 at 14:00
  • `fb://profile/113810631976867` still works because it is an organization page, but if I insert userID for accessing user's profile page -> I get "page not found"! Any solutions? – virusss8 Jun 24 '16 at 09:30
  • http://stackoverflow.com/questions/37199854/swift-open-facebook-profile-by-facebookuid @virusss8 – Thomas Jul 14 '16 at 16:53
  • 4
    In iOS 9 remember to whitelist the scheme by adding the array key `LSApplicationQueriesSchemes` in your Info.plist and the item `fb` as a string item inside the array. – Leandro Fournier Jul 26 '16 at 22:45
  • 1
    Remark with the sample code: Instead of going to the facebook homepage "https://www.facebook.com" when the facebook app is not installed, going to your apps facebookpage "https://facebook.com/myAppsPage" might be more desirable. – Joris van Liempd iDeveloper Sep 26 '16 at 06:34
  • A trailing slash / seems necessary in fb://profile/PageID, so it must be fp://profile/PageID/. – NerdyGeek Feb 07 '19 at 19:08
  • 1
    "fb://profile?id=PageName" working iOS 14 with FB – Pranavan SP Dec 26 '20 at 18:56
38

It seems that since the last answer Facebook have changed the scheme for pages (profile is same as before)

the page_id is now the part of URL query. So in order to redirect to fb app, the url has to be in the format:

fb://page/?id=your_page_numeric_id

Also make sure to whitelist fb scheme in your apps Info.plist

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fb</string>
</array>

In case there will be any further modifications or you don't know your facebook page's page_id, you can extract the precise iOS fb URL from your Facebook web page source.

  1. Open your Facebook page in Safari
  2. Right click -> Inspect Element
  3. Search for al:ios:url

You should be able to find:

<meta property="al:ios:url" content="fb://page/?id='your_page_numeric_id'">

Copying this URL into your app and then calling:

guard let facebookURL = NSURL(string: "fb://page/?id=your_page_numeric_id") else {
    return
}

if (UIApplication.sharedApplication().canOpenURL(facebookURL)) {
    UIApplication.sharedApplication().openURL(facebookURL)
} else {

    guard let webpageURL = NSURL(string: "http://facebook.com/yourPage/") else {
        return
    }

    UIApplication.sharedApplication().openURL(webpageURL)
}

should do the job...

TheJeff
  • 3,665
  • 34
  • 52
ambientlight
  • 7,212
  • 3
  • 49
  • 61
  • 1
    Since this answer is over a year old: I can confirm that this method (fb://event?id= in my case) works as of this comment. On iOS 10 and newest Facebook app version. – nickdnk Feb 01 '17 at 23:43
  • somehow it doesn't work for me though and it's Feb 6th , lol; it only open my own page that i'm currently logged in with .. – TheBen Feb 06 '17 at 20:11
  • 1
    Confirmed working for iOS 10. As OP mentioned, you need to open your FB page in Safari to get the correct `al:ios:url` – David T Aug 01 '17 at 14:48
  • Great secret looking for that `al:ios:url` property! I just used it to find how to deeplink a facebook group (not a page). cheers – BigMcLargeHuge Jan 06 '20 at 21:06
30

I was happy to find skladek's answer, so here's my contribution: Swift + other social networks.

I found that I needed 3 forward slashes for twitter, and only 2 for Facebook and Google+, I didn't investigate more than that…

// Go to https://graph.facebook.com/PageName to get your page id
func openFacebookPage() {
    let facebookURL = NSURL(string: "fb://profile?id=PageName")!
    if UIApplication.sharedApplication().canOpenURL(facebookURL) {
        UIApplication.sharedApplication().openURL(facebookURL)
    } else {
        UIApplication.sharedApplication().openURL(NSURL(string: "https://www.facebook.com/PageName")!)
    }
}

func openTwitterProfile() {
    let twitterURL = NSURL(string: "twitter:///user?screen_name=USERNAME")!
    if UIApplication.sharedApplication().canOpenURL(twitterURL) {
        UIApplication.sharedApplication().openURL(twitterURL)
    } else {
        UIApplication.sharedApplication().openURL(NSURL(string: "https://twitter.com/USERNAME")!)
    }
}

func openGooglePlusPage() {
    let googlePlusURL = NSURL(string: "gplus://plus.google.com/u/0/PageId")!
    if UIApplication.sharedApplication().canOpenURL(googlePlusURL) {
        UIApplication.sharedApplication().openURL(googlePlusURL)
    } else {
        UIApplication.sharedApplication().openURL(NSURL(string: "https://plus.google.com/PageId")!)
    }
}

An attempt at refactoring:

struct SocialNetworkUrl {
    let scheme: String
    let page: String

    func openPage() {
        let schemeUrl = NSURL(string: scheme)!
        if UIApplication.sharedApplication().canOpenURL(schemeUrl) {
            UIApplication.sharedApplication().openURL(schemeUrl)
        } else {
            UIApplication.sharedApplication().openURL(NSURL(string: page)!)
        } 
    }
}

enum SocialNetwork {
    case Facebook, GooglePlus, Twitter, Instagram
    func url() -> SocialNetworkUrl {
        switch self {
        case .Facebook: return SocialNetworkUrl(scheme: "fb://profile/PageId", page: "https://www.facebook.com/PageName")
        case .Twitter: return SocialNetworkUrl(scheme: "twitter:///user?screen_name=USERNAME", page: "https://twitter.com/USERNAME")
        case .GooglePlus: return SocialNetworkUrl(scheme: "gplus://plus.google.com/u/0/PageId", page: "https://plus.google.com/PageId")
        case .Instagram: return SocialNetworkUrl(scheme: "instagram://user?username=USERNAME", page:"https://www.instagram.com/USERNAME")
        }
    }
    func openPage() {
        self.url().openPage()
    }
}

Now you can call:

SocialNetwork.Twitter.openPage()
Naveed Ahmad
  • 6,627
  • 2
  • 58
  • 83
Arnaud
  • 17,268
  • 9
  • 65
  • 83
11

For iOS 9, You must whitelist the url's that your app will call out to using the LSApplicationQueriesSchemes key in your Info.plist.

Check here.

<key>LSApplicationQueriesSchemes</key>
    <array>
     <string>fb</string>
     <string>fbapi</string>
     <string>fbauth2</string>
     <string>fbshareextension</string>
     <string>fb-messenger-api</string>
     <string>twitter</string>
     <string>whatsapp</string>
     <string>wechat</string>
     <string>line</string>
     <string>instagram</string>
     <string>kakaotalk</string>
     <string>mqq</string>
     <string>vk</string>
     <string>comgooglemaps</string>
    </array>
Community
  • 1
  • 1
Emre
  • 295
  • 2
  • 9
3

I just found this URL scheme fb://profile?id={userid/username/pageid}, and it is working fine as of 30 Aug 2020.

eg: fb://profile?id=4 OR fb://profile?id=zuck

Warning: Please use at your own risk as this is not documented in Facebook developers documentation. This URL scheme may be removed by Facebook app in the future without any warning and indication.

luke77
  • 2,255
  • 2
  • 18
  • 30
1

After much testing I have found one of the most effective solutions:

NSString *facebookID = @"XXXXXXXXXX";
NSString *facebookURL = @"www.facebook.com/XXXXXXXX";
NSURL *urlModal = [NSURL URLWithString:[NSString stringWithFormat:@"fb://facewebmodal/f?href=%@", facebookURL]];
NSURL *urlWithID = [NSURL URLWithString:[NSString stringWithFormat:@"fb://page/%@", facebookID]]; // or "fb://profile/%@" or another type of ID
NSURL *url = [NSURL URLWithString:facebook_url];


if(facebookID && !facebookID.isEmpty && [[UIApplication sharedApplication] canOpenURL:urlWithID]) {
    // open facebook app with facebookID
    [[UIApplication sharedApplication] openURL:urlWithID];
} else if (facebookURL && !facebookURL.isEmpty && [[UIApplication sharedApplication] canOpenURL:urlModal]) {
    // open facebook app with facebookUrl
    [[UIApplication sharedApplication] openURL:urlModal];
} else if (![[UIApplication sharedApplication] openURL:url]) {
    // somehow open
    NSLog(@"%@%@",@"Failed to open url:",[url description]);
}

Order of sentences "if else" vary to your liking ...

Enjoy it!! :-)

Javi AP
  • 392
  • 3
  • 8
1

Swift 2.3

For code you can use this example:

Note: If you can't find the facebook profile id can you use the follow site to do it (https://findmyfbid.com/)

func goFacebook() {
    let profileID = "" //Here put profile id Example:'62260589592'
    let facebookURL = NSURL(string: "fb://profile/\(profileID)")!

    if UIApplication.sharedApplication().canOpenURL(facebookURL) {
        UIApplication.sharedApplication().openURL(facebookURL)
    } else {
        UIApplication.sharedApplication().openURL(NSURL(string: "https://www.facebook.com/PageName")!)
    }
}

For Info.plist you need set the follow parameters:

Note: For open the Info.plist on Source Mode

-Go to Info.plist file and click right

-Select "Open As"

-Select "Source Code"

-Put code above of another

<key>LSApplicationQueriesSchemes</key>
<array>
 <string>fb</string>
 <string>fbapi</string>
 <string>fbauth2</string>
 <string>fbshareextension</string>
 <string>fb-messenger-api</string>
</array>
Cristian Mora
  • 1,813
  • 2
  • 19
  • 27
1

// Swift 5, you can use this function to open fb page/profile

       func openFacebookPage() {
            let facebookURL = NSURL(string: "fb://profile/PageId")!
            if UIApplication.shared.canOpenURL(facebookURL as URL) {
            UIApplication.shared.open(facebookURL as URL)
        } else {
            UIApplication.shared.open(NSURL(string: 
             "https://www.facebook.com/PageName")! as URL)
           }
      }
//By Using PageId or name only
      func openFacebookPage(pageID:String) {
            let facebookURL = NSURL(string: "fb://profile/\(pageID)")!
            if UIApplication.shared.canOpenURL(facebookURL as URL) {
            UIApplication.shared.open(facebookURL as URL)
         } else {
             UIApplication.shared.open(NSURL(string: "https://www.facebook.com/\ 
           (pageID)")! as URL)
         }
     }
//By Using Url only
     func openFacebookPageURL(url:String) {
               let facebookURL = NSURL(string: url)!
               if UIApplication.shared.canOpenURL(facebookURL as URL) {
               UIApplication.shared.open(facebookURL as URL)
            } else {
               UIApplication.shared.open(NSURL(string: url)! as URL)
            }
        }