获取Twitter粉丝并关注ios7 [英] Get Twitter Followers and following in ios7

查看:99
本文介绍了获取Twitter粉丝并关注ios7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Twitter API获取关注者和关注者。

i have used Twitter API to get followers and following.

我写了这段代码。获取粉丝和关注

i have write this code. to get followers and following

-(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超出空数组的边界'**

NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'**

为什么会发生这种情况。请帮我解决这个问题。

why this is happening. please help me to sort out this.

推荐答案

最后我得到了解决方案。

Finally i get the solution.

以下是获取Twitter粉丝和以下姓名的解决方案

Here is the Solution for get the Twitter Followers and Following Names

首先,您需要获取有效的用户名。这个名称你可以从

First you need to get the Valid Username. and that name you can get it from

获得这个代码。

NSString * username = [FHSTwitterEngine sharedEngine].authenticatedUsername;

使用该用户名,您可以得到任何您想要的东西

using that username you can get whatever you want

 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"]);

    }

这篇关于获取Twitter粉丝并关注ios7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆