Wojtek @suda Siudzinski


ObjC/Python/Node developer, DIY hacker, rookie designer, 3D print junkie. CEO @ Issue Stand, Senior Full Stack @ Particle.io


Advanced Twitter integration in iOS 5

Now everything is about sharing and iOS 5 gives us built-in Twitter support so you can do it even easier.

But there are two ways to use it:

  • TWTweetComposeViewController - simple composer, similar to SMS and Email composers
  • TWRequest - NSURLRequest like class for Twitter requests

First one is great when you just want to set Twitter status and you can read how to use it at Ray Wenderlich's blog.
If you need to do more, like fetch user's followers or get his avatar, you need to do a little bit more code.

First you have to access user's accounts via ACAccountStore class. You do this by sending requestAccessToAccountsWithType:withCompletionHandler: message to ACAccountStore instance. This will show a dialog, asking user to allow access to his Twitter accounts. Then if it's all ok, you just store them in NSArray.
It's also important to observe ACAccountStoreDidChangeNotification, so you can update accounts when user adds or removes them from system.

- (void)initTwitter {
    // Check if current iOS supports Twitter
    if (NSClassFromString(@"ACAccountStore")) {
        accountStore = [[ACAccountStore alloc] init];
        ACAccountType *twitterType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
        // This will show a dialog asking user to allow access to his Twitter accounts
        [accountStore requestAccessToAccountsWithType:twitterType
                                withCompletionHandler:^(BOOL granted, NSError *error) {
                                    if (granted) {
                                        ACAccountType *twitterType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
                                        // twitterAccounts array will hold all user's Twitter accounts
                                        twitterAccounts = [accountStore accountsWithAccountType:twitterType];                                   
                                    } else {
                                        // User denied access to his Twitter accounts
                                    }
                                }];

        // When user adds/removes accounts, your app have to be notified about that and update its UI
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(twitterAccountsChanged:) 
                                                     name:ACAccountStoreDidChangeNotification object:nil];
    } else {
        // This iOS verion doesn't support Twitter. Use 3rd party library
    }
}

- (void)twitterAccountsChanged:(NSNotification *)notif {  
    ACAccountType *twitterType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    [accountStore requestAccessToAccountsWithType:twitterType
                            withCompletionHandler:^(BOOL granted, NSError *error) {
        if (granted) {
            ACAccountType *twitterType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
            twitterAccounts = [accountStore accountsWithAccountType:twitterType];
        }
   }];
}

After this, you just send a request to Twitter using one of user's accounts:

if (NSClassFromString(@"ACAccountStore")) {  
    // https://dev.twitter.com/docs/api
    NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"];
    NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"Hello world!", @"status", nil];

    TWRequest *tweet = [[TWRequest alloc] initWithURL:url parameters:parameters requestMethod:TWRequestMethodPOST];
    // Set account selected by user
    tweet.account = [twitterAccounts objectAtIndex:0];
    [tweet performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        // Handle errors or success
    }];
}

See? It isn't that bad :) You can also add Twitter accounts to system and couple more cool things, but you can lean this from Session 124: Twitter Integration from WWDC 2011 by Glen Steele and Scott Herz.

comments powered by Disqus