Tag Archives: Facebook

How to open the Facebook App from your own iOS App

Thanks to Custom URL Schemes, iOS Apps can register themselves and be “woken up” by another app (such as yours). Many can even accept parameters if you do.

Opening another app happens via the UIApplication class, and an NSURL as parameter.

Here’s how we open the Facebook App and direct it to open my Pinkstone Pictures page in it:

- (IBAction)openFacebook:(id)sender {
    
    // open the Facebook App
    if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"fb://profile/429759140452016"]]) {
        
        // opening the app didn't work - let's open Safari
        if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.facebook.com/pinkstonepictures"]]) {
            
            // nothing works - perhaps we're not online
            NSLog(@"Note to self: find a different hobby");
        }
    }
}

If the app is not installed nothing happens – which may mean that the app is not installed. As a fallback we’re opening the same page with Safari. If that also doesn’t work, a log message is displayed.

Note that to pass your page into the Facebook App you need your Page or Profile ID. There are several ways to find those:

To find out which other apps you can opened and to figure out which scheme they follow:

To register a Custom URL Scheme in your own app:





How to create a Facebook Like button in your iOS App

Adding a Facebook Like button to a website in HTML is extremely easy, thanks to Facebook’s Developer API. But it’s not as straightforward to implement it in an iOS App.

Thanks to UIWebViews we can create a small web view, perhaps 80 pixels tall, and load the Facebook Like button into it. Here’s how:

This content is for members only.

Now you can react however you see fit: open a URL in Safari, open the Facebook App, etc.

Another thing of note: since the button will be pulled in from Facebook.com, it will not be displayed when a user is not online. Check via UIWebViewDelegate and if the URL can’t be loaded present an image from your bundle instead.