Open link to Facebook page from iOS app Open link to Facebook page from iOS app ios ios

Open link to Facebook page from iOS app


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"]];}


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...


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 idfunc 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()