Search code examples
iosxcodefacebookibaction

How to send user to Facebook page on button click


I'm new to iOS development so please bear with me..

I'm in the process of creating a simple app. There is a facebook button and I'd like that button to send the user to my facebook page, either in the browser or if they have the facebook app open that up.

I know there's probably a lot to explain, but if someone could point me in the right direction that would be great, thanks.


Solution

  • As of the 4.0 update for Facebook, you can jump to pages with this URI schema:

    fb://page/{fbid}
    

    Just make sure you check to make sure that the device you're working with can respond to it:

    [[UIApplication sharedApplication] canOpenURL:[NSURL urlWithString:@"fb://page/{fbid}"]];
    

    Your method might end up looking something like this:

    - (IBAction)openFBPage:(id)sender {
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL urlWithString:@"fb://page/{fbid}"]]) {
            [[UIApplication sharedApplication] openURL:[NSURL urlWithString:@"fb://page/{fbid}"]];
        } else {
            NSLog(@"Facebook isn't installed.");
            // or do the sensible thing and openURL the HTTP version of the page in safari
    }
    

    ... and you'd link the above method with your button in Interface Builder.