Tuesday, October 2, 2012

"Follow us on Twitter" button integration in iOS Application

Hello Guyz, In this section i will show you the two different way to integrate "Follow us on Twitter" button from inside our iOS App. If you are a twitter user then you probably got what i am talking about. "Follow us" button is very popular in web apps, by integrating it we can directly follow some people or organization depending on the twitter screen name we choose to follow and in this section i will show you how we can integrate it from inside our iOS apps.

Method 1: Without using any API
 Without plugging into native twitter app or  using any other API in code, we could simple open a URL to do the job of Following some one in twitter.
-(IBAction)FollowUsOnTwitter:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://twitter.com/intent/user?screen_name=%@",TwitterAccountName]]];
}
Note : TwitterAccountName = your twitter account name you wish to follow, and it will open to follow that account in the browser.

Method 2: Using Twitter Framework for iOS 5 and later
  To be noted here is that Twitter framework is available  only for  iOS 5 and later version.
- First of all , add "Twitter.framework" and "Accounts.framework" to your project.
- now include #import <Accounts/Accounts.h> and #import <Twitter/Twitter.h> to your .h file.

-(IBAction)FollowUsOnTwitter:(id)sender {
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore  

                                                           accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 
    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
        if(granted) {
            // Get the list of Twitter accounts.
            NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];     
            // here, I'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. see Note section below if there are multiple accounts

               if ([accountsArray count] > 0) {
                 // Grab the initial Twitter account to tweet from.
                ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
                NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
                [tempDict setValue:@"TwitterAccountName" forKey:@"screen_name"];
                [tempDict setValue:@"true" forKey:@"follow"];
                TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.twitter.com/1/friendships/create.json"]  parameters:tempDict requestMethod:TWRequestMethodPOST];
                [postRequest setAccount:twitterAccount];
                [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                    NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]];
                    NSLog(@"%@", output);
                    if ([urlResponse statusCode] == 200) {
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Follow us successfull" message:nil delegate:nil cancelButtonTitle:@"Thanx" otherButtonTitles:nil, nil];
                        [alert show];
                    }
                    else {
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Follow us Failed" message:nil delegate:nil cancelButtonTitle:@"Thanx" otherButtonTitles:nil, nil];
                        [alert show];
                    }
                   
                }];
            }
        }
    }];
}

Note : If there are multiple twitter account in your device then you probably can display action sheet with multiple user screen name on it and user be able to select account as per his wish and we can track user selection in UIActionSheet delegate method.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];  
UIActionSheet *actions = [[UIActionSheet alloc] initWithTitle:@"Choose Account to Use" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil];    
            for (ACAccount *oneAccount in accountsArray){
                [actions addButtonWithTitle:oneAccount.username];
               } 
            [actions showInView:self.view];

Now, we can track which account (index) is selected in UIActionSheet delegate method and perform the "Follow us" code integration from there. Make accountsArray global  so that we can find which account has been selected in delegate method tracking selected ActionSheet button index.


No comments:

Post a Comment