2013-12-18 87 views
1

我已經使用Twitter API獲得關注者和關注。獲取Twitter關注者和關注ios7

我已經寫了這段代碼。讓追隨者和以下

-(void)getTwitterAccounts 
{ 
    ACAccountStore *accountStore = [[ACAccountStore alloc] init]; 
    // Create an account type that ensures Twitter accounts are retrieved. 
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 
    // let's request access and fetch the accounts 
    [accountStore requestAccessToAccountsWithType:accountType 
      withCompletionHandler:^(BOOL granted, NSError *error) 


    { 
           // check that the user granted us access and there were no errors (such as no accounts added on the users device) 
           if (granted && !error) 
           { 
            NSArray *accountsArray = [accountStore accountsWithAccountType:accountType]; 
            if ([accountsArray count] > 1) { 
             // a user may have one or more accounts added to their device 
             // you need to either show a prompt or a separate view to have a user select the account(s) you need to get the followers and friends for 
            } else { 
             [self getTwitterFriendsForAccount:[accountsArray objectAtIndex:0]]; 
            } 
           } else { 
            // handle error (show alert with information that the user has not granted your app access, etc.) 
           } 
          }]; 
} 



-(void)getTwitterFriendsForAccount:(ACAccount*)account 
{ 
    // In this case I am creating a dictionary for the account 
    // Add the account screen name 
    NSMutableDictionary *accountDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:account.username, @"screen_name", nil]; 
    // Add the user id (I needed it in my case, but it's not necessary for doing the requests) 
    [accountDictionary setObject:[[[account dictionaryWithValuesForKeys:[NSArray arrayWithObject:@"properties"]] objectForKey:@"properties"] objectForKey:@"user_id"] forKey:@"user_id"]; 
    // Setup the URL, as you can see it's just Twitter's own API url scheme. In this case we want to receive it in JSON 
    NSURL *followingURL = [NSURL URLWithString:@"http://api.twitter.com/1/friends/ids.json"]; 
    // Pass in the parameters (basically '.ids.json?screen_name=[screen_name]') 
    NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:account.username, @"screen_name", nil]; 
    // Setup the request 
    TWRequest *twitterRequest = [[TWRequest alloc] initWithURL:followingURL 
                parameters:parameters 
               requestMethod:TWRequestMethodGET]; 
    // This is important! Set the account for the request so we can do an authenticated request. Without this you cannot get the followers for private accounts and Twitter may also return an error if you're doing too many requests 
    [twitterRequest setAccount:account]; 
    // Perform the request for Twitter friends 
    [twitterRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
     if (error) { 
      // deal with any errors - keep in mind, though you may receive a valid response that contains an error, so you may want to look at the response and ensure no 'error:' key is present in the dictionary 
     } 
     NSError *jsonError = nil; 
     // Convert the response into a dictionary 
     NSDictionary *twitterFriends = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&jsonError]; 
     // Grab the Ids that Twitter returned and add them to the dictionary we created earlier 
     [accountDictionary setObject:[twitterFriends objectForKey:@"ids"] forKey:@"friends_ids"]; 
     NSLog(@"%@", accountDictionary); 
    }]; 
} 

但是這個代碼不工作

給我的錯誤

NSRangeException '原因是:' * - [__ NSArrayI objectAtIndex:]:索引0超越界限空陣列'**

爲什麼會發生這種情況。請幫我解決這個問題。

回答

2

試試這個。看起來你試圖使用舊的Twitter的API api

ACAccountStore *accountStore = [[ACAccountStore alloc] init]; 
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error){ 
    if (granted) { 
     NSArray *accounts = [accountStore accountsWithAccountType:accountType]; 
     // Check if the users has setup at least one Twitter account 
     if (accounts.count > 0) 
     { 
      ACAccount *twitterAccount = [accounts objectAtIndex:0]; 

      for(ACAccount *t in accounts) 
      { 
       if([t.username isEqualToString:username]) 
       { 
        twitterAccount = t; 
        break; 
       } 
      } 

      SLRequest *twitterInfoRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/friends/ids.json?"] parameters:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%@", username], @"screen_name", @"-1", @"cursor", nil]]; 
      [twitterInfoRequest setAccount:twitterAccount]; 
      // Making the request 
      [twitterInfoRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        // Check if we reached the reate limit 
        if ([urlResponse statusCode] == 429) { 
         NSLog(@"Rate limit reached"); 
         return; 
        } 
        // Check if there was an error 
        if (error) { 
         NSLog(@"Error: %@", error.localizedDescription); 
         return; 
        } 
        // Check if there is some response data 
        if (responseData) { 
         NSError *error = nil; 
         NSArray *TWData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error]; 


        } 
       }); 
      }]; 
     } 
    } else { 
     NSLog(@"No access granted"); 
    } 
}]; 
+0

此代碼獲取追隨者,後面的名... – Jitendra

+0

然後,你將需要請求這個(通過分頁),以獲得SLRequest名* twitterUsersRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@「https://api.twitter.com/1.1/users/lookup.json」] parameters:[NSDictionary dictionaryWithObjectsAndKeys:[first100 componentsJoinedByString:@「, 「],@」user_id「,@」false「,@」include_entities「,nil]]; – nerowolfe

+0

請發佈代碼。和這個代碼中的usename是什麼。我應該在這裏通過什麼... – Jitendra

4

最後我得到的解決方案。

這裏是讓Twitter的追隨者和名稱如下

首先你需要得到有效的用戶名的解決方案。而這個名字你可以從

這個代碼。

NSString * username = [FHSTwitterEngine sharedEngine].authenticatedUsername; 

使用的用戶名,你可以得到任何你想要的

NSMutableDictionary * dict1 = [[FHSTwitterEngine sharedEngine]listFriendsForUser:username isID:NO withCursor:@"-1"]; 

    NSLog(@"====> %@",[dict1 objectForKey:@"users"]);  // Here You get all the data 
    NSMutableArray *array=[dict1 objectForKey:@"users"]; 
    for(int i=0;i<[array count];i++) 
    { 
     NSLog(@"names:%@",[[array objectAtIndex:i]objectForKey:@"name"]); 

    } 
+0

謝謝兄弟... –

相關問題