Episode #56

Twitter Integration

16 minutes
Published on March 7, 2013

This video is only available to subscribers. Get access to this video and 572 others.

In this episode we implement the Social framework in order to integrate with Twitter. Using the provided framework, we issue an authenticated API call to get a list of Twitter followers for a given account, as well as compose a tweet with the new SLComposeViewController.

Episode Links

Maintaining a reference to the AccountStore

The account store is used to gain access to Twitter accounts that are saved on the device. Initially it will prompt the user for access and after it is granted, you can pull the accounts out of the store.

Creating the store is easy:

self.accountsStore = [[ACAccountStore alloc] init];

You also, however, have to release & rebuild this object if the account store changes. To do that, listen for the ACAccountStoreDidChange notification and rebuild it.

Requesting access to Twitter accounts

    ACAccountType *twitterAccountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    [self.accountStore requestAccessToAccountsWithType:twitterAccountType
                                               options:nil
                                            completion:^(BOOL granted, NSError *error) {
                                                if (granted) {
                                                    NSLog(@"Granted!");
                                                    ACAccount *account = [[self.accountStore accountsWithAccountType:twitterAccountType] lastObject];
                                                    [self fetchTwitterFollowersForAccount:account];
                                                } else {
                                                    NSLog(@"No permission :(  %@", error);
                                                }
                                            }];

Fetching Twitter followers for an account

Once we have an account, we can make authenticated requests to the twitter API using SLRequest. The details of what we can do are documented on Twitter's API Docs.

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    NSURL *url = URLIFY(@"https://api.twitter.com/1.1/followers/list.json");
    id params = @{@"skip_status" : @"1", @"screen_name":@"nsscreencast", @"count": @"100"};
    SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                            requestMethod:SLRequestMethodGET
                                                      URL:url
                                               parameters:params];
    request.account = twitterAccount;
    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        if (responseData) {
            if (urlResponse.statusCode >= 200 && urlResponse.statusCode < 300) {
                NSError *jsonError = nil;
                NSDictionary *followerData = [NSJSONSerialization JSONObjectWithData:responseData
                                                                             options:NSJSONReadingAllowFragments
                                                                               error:&jsonError];
                if (followerData) {
                    NSLog(@"Response: %@", followerData);
                    self.followerDictionary = followerData;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self.tableView reloadData];
                    });
                } else {
                    NSLog(@"JSON Parsing error: %@", jsonError);
                }
            } else {
                NSLog(@"Server returned HTTP %d", urlResponse.statusCode);
            }
        } else {
            NSLog(@"Something went wrong: %@", [error localizedDescription]);
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        });
    }];

Composing a Tweet using SLComposeViewController

    NSString *tweetText = @"I'm tweeting from iOS 6 😏";
    SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
    [tweetSheet setInitialText:tweetText];
    [self presentViewController:tweetSheet
                       animated:YES
                     completion:nil];