Open a facebook link by native Facebook app on iOS

IphoneIosXcodeFacebook

Iphone Problem Overview


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.

Iphone Solutions


Solution 1 - Iphone

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

Solution 2 - Iphone

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

Solution 3 - Iphone

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.

Solution 4 - Iphone

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.

Solution 5 - Iphone

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

Solution 6 - Iphone

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)")
        }
 }

Solution 7 - Iphone

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]

Solution 8 - Iphone

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

Solution 9 - Iphone

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)
}

Solution 10 - Iphone

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

Solution 11 - Iphone

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvietstoneView Question on Stackoverflow
Solution 1 - IphoneWrightsCSView Answer on Stackoverflow
Solution 2 - IphoneRomano401View Answer on Stackoverflow
Solution 3 - IphonezoulView Answer on Stackoverflow
Solution 4 - IphoneBrad MooreView Answer on Stackoverflow
Solution 5 - IphoneHussoMView Answer on Stackoverflow
Solution 6 - IphoneiPareshView Answer on Stackoverflow
Solution 7 - Iphoneuser3012572View Answer on Stackoverflow
Solution 8 - IphoneDanView Answer on Stackoverflow
Solution 9 - IphoneVikram ChaudharyView Answer on Stackoverflow
Solution 10 - IphoneSaadView Answer on Stackoverflow
Solution 11 - IphoneConfused VorlonView Answer on Stackoverflow