How to get twitter profile picture in ios?

后端 未结 6 1875
悲哀的现实
悲哀的现实 2021-01-03 02:43

I wrote the following code:

NSURL *url = [NSURL URLWithString:@\"http://api.twitter.com/1.1/users/show.json\"];

NSDictionary *params = [NSDictionary diction         


        
6条回答
  •  长情又很酷
    2021-01-03 03:21

    It's profile_image_url_https ;)

    We can access profile image without using Twitter sdk .Using Social framework in iOS we can get using it.

    I use ACAccounts instead of Twitter IOS SDK's like MGTwitterEngine etc....... The twitter account provided in the IPhone Settings will be used.

            if(!accountStore)
                accountStore = [[ACAccountStore alloc] init];
            ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    
            [accountStore
             requestAccessToAccountsWithType:accountType
             options:NULL
             completion:^(BOOL granted, NSError *error) {
                 if (granted) {
                     //  Step 2:  Create a request
                     NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
                     self.twitterAccount = [accountsArray objectAtIndex:0];
                     // NSString *userID = [[twitterAccount valueForKey:@"properties"] valueForKey:@"user_id"];
    
                     NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/users/show.json"];
                     NSDictionary *params = @{@"screen_name" : twitterAccount.username
                                              };
                     SLRequest *request =
                     [SLRequest requestForServiceType:SLServiceTypeTwitter
                                        requestMethod:SLRequestMethodGET
                                                  URL:url
                                           parameters:params];
    
                     //  Attach an account to the request
                     [request setAccount:[accountsArray lastObject]];
    
                     //  Step 3:  Execute the request
                     [request performRequestWithHandler:^(NSData *responseData,
                                                          NSHTTPURLResponse *urlResponse,
                                                          NSError *error) {
                         if (responseData) {
    
                             if (urlResponse.statusCode >= 200 && urlResponse.statusCode < 300) {
                                 [self performSelectorOnMainThread:@selector(twitterdetails:)
                                                        withObject:responseData waitUntilDone:YES];
                             }
                             else {
    
                                 NSLog(@"The response status code is %d", urlResponse.statusCode);
                             }
                         }
                     }];
                 }
                 else
                 {
                     dispatch_async(dispatch_get_main_queue(), ^{
                         [self dismissError:@"Please set up your twitter account in iphone settings"];
    
                     });
                 }
             }];
    
    
    
    -(void)twitterdetails:(NSData *)responseData {
    
        NSError* error = nil;
        NSDictionary* json = [NSJSONSerialization
                              JSONObjectWithData:responseData //1
                              options:NSJSONReadingAllowFragments
                              error:&error];
    
        NSString *name = [json objectForKey:@"name"];
        NSString *scrnm = [json objectForKey:@"screen_name"];
        NSString *twitterid = [json objectForKey:@"id"];
        NSString *prof_img = [json objectForKey:@"profile_image_url"];
        NSString *location = [json objectForKey:@"location"];
    }
    

提交回复
热议问题