Tuesday, July 15, 2014

How to Get Twitter Profile information From iPhone Settings in iOS


-(void)getTwitterSettingsInfo
{
    ACAccountStore *accountStore = [[ACAccountStore alloc] init] ;
    
    ACAccountType *twitterAccountType =  [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    NSArray *twitterAccounts = [accountStore accountsWithAccountType:twitterAccountType];
    if([twitterAccounts count] > 0)
    {
        ACAccount *twitterAccount = [twitterAccounts objectAtIndex:0];
        
        NSString *twitterUserName = twitterAccount.username;
        twillterUserLabel.text = twitterUserName;
        
        if(twitterIdentifier == 10) //Getting iamge if Identfier = 10
        {
            NSError *error;
            NSString * requestString = [NSString stringWithFormat:@"https://api.twitter.com/1.1/users/show.json?screen_name=%@", twitterUserName];
            NSURL *verify = [NSURL URLWithString:requestString];
            NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:verify];
            [[PFTwitterUtils twitter] signRequest:request];
            NSURLResponse *response = nil;
            NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
            
            if ( error == nil){
                NSDictionary* result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
                NSLog(@"%@",result);
                
                NSString * requestString = [result objectForKey:@"profile_image_url_https"];
                
                NSURL *url = [NSURL URLWithString:requestString];
                NSData *imageData = [NSData dataWithContentsOfURL:url];
                [profilepicButton setBackgroundImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal];
                twitterIdentifier = 0;
            }
        }
        
    }
    else
        twillterUserLabel.text = @"Undefined";
    


}

No comments:

Post a Comment