Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add 'Follow us on facebook' button to an iOS app?

I've about completed my iOS app but only need to add two buttons:

  • Follow us on facebook
  • Follow us on twitter

I assumed I would find a couple of simple examples here, but was surprised to find no answer to this at all (yet).

I'm pretty sure I could spend the next few hours trying to figure it out, but thought I'd reach out for a little time-saving help.

Just looking for the code to go behind a button on a view controller (XCode 4.5.1, iOS 6).

I assume the only variable I might need to supply is the company's facebook account name.

Any suggestions? (Thanks in advance!)

like image 984
dcb Avatar asked Feb 28 '13 15:02

dcb


People also ask

How do I get the follow button on my Facebook app?

Make a Follow Button From the App Tap the menu button on the top right, and then scroll down to expand Settings & Privacy. Choose Settings. Tap Profile Settings, followed by Public Posts. In the first section, under the heading Who Can Follow Me, choose Public.

How do I enable followers on Facebook iOS?

Tap in the top right of Facebook. Scroll down and tap Settings. Scroll down and tap Followers and Public Content. Tap Friends or Public below Who Can Follow Me.

How do I change my Facebook Like button to follow button?

To change the Follow button to something else: Open the Facebook page. Click on the 'Edit Follow' button. Select another CTA button option.


2 Answers

First, the URL schem for Facebook: fb://profile/<yourpageid> (source). A URL with this structure will open the Facebook app, if it is installed.

More on iOS URL schemes.

When your button is tapped, you can check if the Facebook is installed:

-(IBAction)fbButtonTap:(id)sender {
    NSURL *fbURL = [[NSURL alloc] initWithString:@"fb://profile/<yourpageid>"];
    // check if app is installed
    if ( ! [[UIApplication sharedApplication] canOpenURL:fbURL] ) {
        // if we get here, we can't open the FB app.
        fbURL = ...; // direct URL on FB website to open in safari 
    }

    [[UIApplication sharedApplication] openURL:fbURL];
}

For twitter, you follow the same basic steps. The Twitter URL scheme for iOS is twitter://user?id=12345 or twitter://user?screen_name=yourname (source). Again, if the Twitter app is not installed, you have open the twitter profile in safari.

As for taking direct actions, I do not think you can do that, since there is no inherent knowledge about any other applciation installed on the device. The best I think you can do is direct users to each respective account.

like image 178
Mike D Avatar answered Oct 17 '22 14:10

Mike D


You could use SLRequest have someone follow you. This will only work on iOS 6 though, on iOS 5 Twitter works but not Facebook.

An example of SLRequest for someone to follow you on Twitter, make sure this is called on the background thread:

ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {

        if (granted) {

            // Get the list of Twitter accounts.
            NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];

            // For the sake of brevity, we'll assume there is only one Twitter account present.
            // You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present.
            if ([accountsArray count] > 0) {
                // Grab the initial Twitter account to tweet from.
                ACAccount *twitterAccount = [accountsArray objectAtIndex:0];

                NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
                [tempDict setValue:@"Your twitter name" forKey:@"screen_name"];
                [tempDict setValue:@"true" forKey:@"follow"];

                SLRequest *followRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://api.twitter.com/1/friendships/create.json"] parameters:tempDict];

                [followRequest setAccount:twitterAccount];
                [followRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                    NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]];
                    NSLog(@"%@", output);
                    if (error) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            //Update UI to show follow request failed
                        });
                    }
                    else {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            //Update UI to show success
                        });
                    }
                }];  
            }
        }
    }];

And for Facebook you just have to change some of it and look at their API and change the request URL.

like image 25
Maximilian Litteral Avatar answered Oct 17 '22 15:10

Maximilian Litteral