Follow a username on Twitter iOS 5+
This easy snippet allows you to specify a username you'd like your users to follow. For instance, in an About page you can add a "Follow Me" button that checks the users logged in twitter accounts and follows you with the first available account.
Simply use:
[self follow:@"shabzcohelp"];
-(void) follow:(NSString *) username { if ([TWTweetComposeViewController canSendTweet]) { // Create account store, followed by a twitter account identifier ACAccountStore *accountStore = [[ACAccountStore alloc] init]; ACAccountType *type = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; [accountStore requestAccessToAccountsWithType:type withCompletionHandler:^(BOOL granted, NSError *error) { if(granted) { NSArray *accountsArray = [accountStore accountsWithAccountType:type]; // Sanity check if ([accountsArray count] > 0) { //Create dictionary to pass to followWithAccountInfo: method NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:username forKey:@"usernameToFollow"]; //Follow the username with your first logged in account [dict setObject:[accountsArray objectAtIndex:0] forKey:@"account"]; [self performSelectorOnMainThread:@selector(followWithAccountInfo:) withObject:dict waitUntilDone:NO]; } else { //No Accounts Available } } }]; } } -(void) followWithAccountInfo:(NSDictionary *) dictionary { ACAccount *acct = [dictionary objectForKey:@"account"]; NSString *username = [dictionary objectForKey:@"usernameToFollow"]; // Build a twitter request for following the username specified TWRequest *postRequest = [[TWRequest alloc] initWithURL: [NSURL URLWithString:@"http://api.twitter.com/1.1/friendships/create.json"] parameters:[NSDictionary dictionaryWithObjectsAndKeys:username, @"screen_name", @"true", @"follow", nil] requestMethod:TWRequestMethodPOST]; // Post the request [postRequest setAccount:acct]; // Block handler to manage the response [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {NSLog(@"%d", urlResponse.statusCode); [postRequest release];}]; }











