65

If the native Facebook app is installed on the iPhone. How do I open a facebook link into the native Facebook app from my app. In the case of opening by Safari, the link is same as:

http://www.facebook.com/AlibabaUS

Thank you.

zoul
  • 102,279
  • 44
  • 260
  • 354
vietstone
  • 8,784
  • 16
  • 52
  • 79
  • 5
    Your way does not work with the new FB app (August 2012)... The fb://profile/ does – yonix Sep 04 '12 at 07:42
  • 9
    To add to what @yonix said, you can find the page id by going to http://graph.facebook.com/AlibabaUS. Even though you want a page, use "profile" in the URL. Also if you think a user might not have the FB app installed, call canOpenURL: before openURL: – Ben Flynn Oct 01 '12 at 22:08

11 Answers11

133

Here are some schemes the Facebook app uses, there are a ton more on the source link:

Example

NSURL *url = [NSURL URLWithString:@"fb://profile/<id>"];
[[UIApplication sharedApplication] openURL:url];

Schemes

fb://profile – Open Facebook app to the user’s profile

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

If before opening this url you want to check wether the user fas the facebook app you can do the following (as explained in another answer below):

if ([[UIApplication sharedApplication] canOpenURL:nsurl]){
    [[UIApplication sharedApplication] openURL:nsurl];
}
else {
    //Open the url as usual
}

Source

http://wiki.akosma.com/IPhone_URL_Schemes#Facebook

Community
  • 1
  • 1
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • can i open this url in safari with login?. I am using Facebook in my whole app. Already i logged in in Facebook. But if i am opening a Facebook url page with safari. Its asking again to Login. How can i solve this?. – sathiamoorthy Sep 24 '12 at 08:26
  • @sathiamoorthy : if you find solution to your question please inform me too thanks :) – W.S Feb 22 '13 at 06:31
  • 3
    Take note, schemes are not officially supported by facebook, they may change/stop working at any moment. – DogDog Aug 19 '13 at 15:49
  • I used some of this implementation, but also use the solution by @Romano401 to find the graphid or page id instead of the visual URL like facebook.com/yourbusinesspage Facebook seems to want the ID passed. – Nick N Jul 30 '14 at 04:34
  • 1
    I'm getting page not found even though I know the id is correct. Any idea why this is? – C0D3 Oct 16 '14 at 23:53
  • 1
    We need to add whitelist in `Info.plist` in iOS 9 too to make it works. See this http://useyourloaf.com/blog/querying-url-schemes-with-canopenurl.html. – haxpor Nov 03 '15 at 05:05
  • This question shows that it is not supported: http://stackoverflow.com/questions/31979367/ios-facebook-custom-url-scheme-to-open-user-profile – Tony Feb 18 '16 at 21:53
  • it gave me a error -canOpenURL: failed for URL: "(null)" - error: "Invalid input URL" – jaskiratjd Oct 27 '16 at 13:07
  • There are new rules for **`canOpenUrl:`**, `LSApplicationQueriesSchemes` – WrightsCS Oct 27 '16 at 18:39
  • 1
    I did the same thing but it doesn't work with the post that contains video. it goes to the Facebook app but show nothing. For example this one `10153231379946729`. Any idea? – hientrq Jan 16 '17 at 04:58
  • Source link is dead. Is there an updated link that has official docs? – alexdriedger Sep 11 '17 at 00:02
  • Anyone know why most of the links in this answer do not deep link anymore (they just open facebook and stay on the feed or whatever current page is)? The only ones working for me right now are feed, notifications and events – TahoeWolverine Mar 11 '20 at 20:40
27

Just use https://graph.facebook.com/(your_username or page name) to get your page ID and after you can see all the detain and your ID

after in your IOS app use :

 NSURL *url = [NSURL URLWithString:@"fb://profile/[your ID]"];
 [[UIApplication sharedApplication] openURL:url];
Hiren
  • 12,720
  • 7
  • 52
  • 72
Romano401
  • 642
  • 8
  • 11
26

To add yonix’s comment as an answer, the old fb://page/… URL no longer works. Apparently it was replaced by fb://profile/…, even though a page is not a profile.

zoul
  • 102,279
  • 44
  • 260
  • 354
10

I did this in MonoTouch in the following method, I'm sure a pure Obj-C based approach wouldn't be too different. I used this inside a class which had changing URLs at times which is why I just didn't put it in a if/elseif statement.

NSString *myUrls = @"fb://profile/100000369031300|http://www.facebook.com/ytn3rd";
NSArray *urls = [myUrls componentsSeparatedByString:@"|"];
for (NSString *url in urls){
    NSURL *nsurl = [NSURL URLWithString:url];
    if ([[UIApplication sharedApplication] canOpenURL:nsurl]){
        [[UIApplication sharedApplication] openURL:nsurl];
        break;
    }
}

The break is not always called before your app changes to Safari/Facebook. I assume your program will halt there and call it when you come back to it.

Community
  • 1
  • 1
Brad Moore
  • 316
  • 5
  • 23
8

I know the question is old, but in support of the above answers i wanted to share my working code. Just add these two methods to whatever class.The code checks if facebook app is installed, if not installed the url is opened in a browser.And if any errors occur when trying to find the profileId, the page will be opened in a browser. Just pass the url(example, http://www.facebook.com/AlibabaUS) to openUrl: and it will do all the magic. Hope it helps someone!.

NOTE: UrlUtils was the class that had the code for me, you might have to change it to something else to suite your needs.

+(void) openUrlInBrowser:(NSString *) url
{
    if (url.length > 0) {
        NSURL *linkUrl = [NSURL URLWithString:url];
        [[UIApplication sharedApplication] openURL:linkUrl];
    }
}
+(void) openUrl:(NSString *) urlString
{

    //check if facebook app exists
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fb://"]]) {

        // Facebook app installed
        NSArray *tokens = [urlString componentsSeparatedByString:@"/"];
        NSString *profileName = [tokens lastObject];

        //call graph api
        NSURL *apiUrl = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@",profileName]];
        NSData *apiResponse = [NSData dataWithContentsOfURL:apiUrl];

        NSError *error = nil;
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:apiResponse options:NSJSONReadingMutableContainers error:&error];

        //check for parse error
        if (error == nil) {

            NSString *profileId = [jsonDict objectForKey:@"id"];

            if (profileId.length > 0) {//make sure id key is actually available
                NSURL *fbUrl = [NSURL URLWithString:[NSString stringWithFormat:@"fb://profile/%@",profileId]];
                [[UIApplication sharedApplication] openURL:fbUrl];
            }
            else{
                [UrlUtils openUrlInBrowser:urlString];
            }

        }
        else{//parse error occured
            [UrlUtils openUrlInBrowser:urlString];
        }

    }
    else{//facebook app not installed
        [UrlUtils openUrlInBrowser:urlString];
    }

}
HussoM
  • 1,272
  • 13
  • 7
  • This will not work anymore: `"message": "(#803) Cannot query users by their username (user.name)"` – Jasper Jul 27 '15 at 12:02
4

swift 3

if let url = URL(string: "fb://profile/<id>") {
        if #available(iOS 10, *) {
            UIApplication.shared.open(url, options: [:],completionHandler: { (success) in
                print("Open fb://profile/<id>: \(success)")
            })
        } else {
            let success = UIApplication.shared.openURL(url)
            print("Open fb://profile/<id>: \(success)")
        }
 }
iParesh
  • 2,338
  • 1
  • 18
  • 30
  • How will get this id? – Neelam Pursnani Jul 10 '18 at 11:46
  • Here is link may be help you https://developers.facebook.com/docs/apps/upgrading#upgrading_v2_0_user_ids – iParesh Jul 11 '18 at 04:22
  • when you create fb account that time fb create unique id of user. above example what we have do we open user profile on native fb app – iParesh Jul 11 '18 at 04:28
  • Okay. What i want is, How to open other's profile in facebook native app? I search a lot but not getting anything. Details of question is here: https://stackoverflow.com/questions/51207496/not-able-to-open-particular-user-profile-page-in-native-ios-facebook-app – Neelam Pursnani Jul 11 '18 at 05:16
  • Are you using fb sdk? – iParesh Jul 11 '18 at 06:04
  • No, I am getting user's fb profile details in response through API am getting below details: email = "harixxx@yahoo.com"; id = 48xxx; name = "Harixxx H"; nickname = ""; "provider_id" = 17747xxxxxxx7495; slug = facebook; username = "https://www.facebook.com/app_scoped_user_id/YXNpZADpBWEVsVzBpdXJ4UXNpxxxxxxxxxxxxzcHVpY1ZAod25WxxxxxxxxxxxxVRQcTkyYTJKNjl4ckFZAbnlnekNINnhBV2JXTxxxxxxxxxxxxxxxxxxxxxxxxxW9fMmd0TFdv/ am passing username(link) in fb://facewebmodal/f?href=\(username) – Neelam Pursnani Jul 11 '18 at 06:40
  • this is working fine for browser but not opening profile in fb native app – Neelam Pursnani Jul 11 '18 at 06:44
  • Have you try to set LSApplicationQueriesSchemes in Info.plist file? if no just try to set and check. here is link https://stackoverflow.com/questions/37199854/swift-open-facebook-profile-by-facebookuid – iParesh Jul 11 '18 at 07:03
  • I have already added this 4 things: fb fbapi fbauth2 fbshareextension fb-messenger-api – Neelam Pursnani Jul 11 '18 at 07:17
1

If the Facebook application is logged in, the page will be opened when executing the following code. If the Facebook application is not logged in when executing the code, the user will then be redirected to the Facebook app to login and then after connecting the Facebook is not redirected to the page!

NSURL *fbNativeAppURL = [NSURL URLWithString:@"fb://page/yourPageIDHere"] [[UIApplication sharedApplication] openURL:fbNativeAppURL]
Don Cruickshank
  • 5,641
  • 6
  • 48
  • 48
1

Just verified this today, but if you are trying to open a Facebook page, you can use "fb://page/{Page ID}"

Page ID can be found under your page in the about section near the bottom.

Specific to my use case, in Xamarin.Forms, you can use this snippet to open in the app if available, otherwise in the browser.

Device.OpenUri(new Uri("fb://page/{id}"));

Dan
  • 1,101
  • 1
  • 9
  • 30
1

Previous method fb://profile/[ID] is not working.

try fb://profile?id=[ID]

Example of usage:

// Open Profile or page
// To open in Application
if UIApplication.shared.canOpenURL(URL(string: "fb://profile?id=vikramchaudhary.ios")!) {
    UIApplication.shared.open(URL(string: "fb://profile?id=vikramchaudhary.ios")!, options: [:], completionHandler: nil)
} else {
    // To open in safari
    UIApplication.shared.open(URL(string: "https://facebook.com/vikramchaudhary.ios/")!, options: [:], completionHandler: nil)
}
Vikram Chaudhary
  • 580
  • 4
  • 14
0

This will open the URL in Safari:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.facebook.com/comments.php?href=http://wombeta.jiffysoftware.com/ViewWOMPrint.aspx?WPID=317"]];

For the iphone, you can launch the Facebook app if installed by using a url starting with fb://

More information can be found here: http://iphonedevtools.com/?p=302 also here: http://wiki.akosma.com/IPhone_URL_Schemes#Facebook

Stolen from the above site:

fb://profile – Open Facebook app to the user’s profile
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

To launch the above URLs:

NSURL *theURL = [NSURL URLWithString:@"fb://<insert function here>"];
[[UIApplication sharedApplication] openURL:theURL];
typeoneerror
  • 55,990
  • 32
  • 132
  • 223
Saad
  • 8,857
  • 2
  • 41
  • 51
  • Is it possible to check in Simulator 6.1 ? – Chintan Jul 31 '13 at 12:29
  • You can't add any application on simulator. That's why it will not run on simulator – Saad Jul 31 '13 at 16:07
  • It looks like in iOS 7 http facebook URLs are opened in the native Facebook app even if they won't work (e.g. using an alias instead of a UID). I should probably file a radar. – gerry3 Oct 17 '13 at 22:39
0

2020 answer

Just open the url. Facebook automatically registers for deep links.

let url = URL(string:"https://www.facebook.com/TheGoodLordAbove")!
UIApplication.shared.open(url,completionHandler:nil)

this opens in the facebook app if installed, and in your default browser otherwise

Confused Vorlon
  • 9,659
  • 3
  • 46
  • 49