Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone-Twitter API获得用户关注/关注_Iphone_Objective C_Ios_Ios5_Twitter - Fatal编程技术网

iPhone-Twitter API获得用户关注/关注

iPhone-Twitter API获得用户关注/关注,iphone,objective-c,ios,ios5,twitter,Iphone,Objective C,Ios,Ios5,Twitter,我希望能够使用ios 5的Twitter API将所有用户关注者和后续用户名放入NSDictionary 不过我遇到了一个路障。我不知道如何使用Twitter API来做这件事。。。但是我的主要问题是首先获取用户的用户名。当我甚至不知道用户的用户名时,如何发出API请求来查找此用户的追随者 有没有人能给我举个例子,让你的Twitter用户关注你和关注你 PS:我已经添加了Twitter框架,并导入了它,它是苹果的Twitter API和Twitter自己的API的组合。一旦你读了代码,它就相当直

我希望能够使用ios 5的Twitter API将所有用户关注者和后续用户名放入
NSDictionary

不过我遇到了一个路障。我不知道如何使用Twitter API来做这件事。。。但是我的主要问题是首先获取用户的用户名。当我甚至不知道用户的用户名时,如何发出API请求来查找此用户的追随者

有没有人能给我举个例子,让你的Twitter用户关注你和关注你


PS:我已经添加了Twitter框架,并导入了它,它是苹果的Twitter API和Twitter自己的API的组合。一旦你读了代码,它就相当直截了当了。我将提供示例代码,说明如何获取Twitter帐户的“好友”(这是用户关注的人的术语),这应该足以让您使用一种方法来获取帐户的关注者

首先,添加
账户
推特
框架

现在,让我们获取用户设备上的Twitter帐户

#import <Accounts/Accounts.h>

-(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.)
                                }
    }];
}
#导入命令:

#导入
-(无效)getTwitterFriendsForAccount:(ACAccount*)帐户{
//在本例中,我正在为该帐户创建字典
//添加帐户屏幕名称
NSMutableDictionary*accountDictionary=[NSMutableDictionary dictionaryWithObjectsAndKeys:account.username,@“screen_name”,nil];
//添加用户id(在我的案例中我需要它,但执行请求时不需要它)
[accountDictionary setObject:[[account Dictionary WithValuesForkeys:[NSArray arrayWithObject:@“properties”]]objectForKey:@“properties”]objectForKey:@“user_id”]forKey:@“user_id”];
//设置URL,正如您所看到的,它只是Twitter自己的API URL方案
NSURL*以下URL=[NSURL URLWithString:@”http://api.twitter.com/1/friends/ids.json"];
//传入参数(基本上是'.ids.json?screen_name=[screen_name]'))
NSDictionary*参数=[NSDictionary Dictionary WithObjectsAndKeys:account.username,@“screen_name”,nil];
//设置请求
TWRequest*twitterRequest=[[TWRequest alloc]initWithURL:followURL
参数:参数
requestMethod:TWRequestMethodGET];
//这很重要!请设置请求的帐户,以便我们可以执行经过身份验证的请求。如果没有此设置,您将无法获得私人帐户的关注者,如果您执行的请求太多,Twitter也可能会返回错误
[TwitterRequestSetAccount:account];
//执行对Twitter好友的请求
[twitterRequest performRequestWithHandler:^(NSData*responseData,NSHTTPURLRResponse*URLRResponse,NSError*错误){
如果(错误){
//处理任何错误-请记住,尽管您可能会收到包含错误的有效响应,因此您可能希望查看响应并确保字典中不存在“error:”键
}
n错误*jsonError=nil;
//将响应转换为字典
NSDictionary*twitterFriends=[NSJSONSerialization JSONObject WithData:responseData选项:NSJSONWriting预打印错误:&jsonError];
//抓取Twitter返回的ID并将它们添加到我们之前创建的字典中
[accountDictionary setObject:[twitterFriends objectForKey:@“ids”]forKey:@“friends_ids”];
NSLog(@“%@”,accountDictionary);
}];
}
当你想要一个帐户的追随者,它几乎是一样的。。。简单使用URL
http://api.twitter.com/1/followers/ids.format
并通过

希望这能给你一个良好的开端

更新:

正如评论中指出的,您应该使用更新的API调用:
https://api.twitter.com/1.1/followers/list.json

使用

#导入“FHSTwitterEngine.h”

添加SystemConfiguration.framework

将以下代码写入viewDidLoad(用于oauth登录)

UIButton*logIn=[UIButton-button-with-type:uibuttontyperoundrect];
logIn.frame=CGRectMake(100100100100);
[登录设置标题:@“登录”状态:UIControlStateNormal];
[logIn addTarget:self action:@selector(showLoginWindow:)for controlEvents:UIControlEventTouchUpInside];
[self.view addSubview:logIn];
[[FHSTwitterEngine sharedEngine]永久设置消费者市场:@“Xg3ACDprWAH8loEPjMzRg”和机密:@“9LwYDxw1iTc6D9ebHdrYCZrJP4lJhQv5uf4ueiPHvJ0”];
[[FHSTwitterEngine sharedEngine]setDelegate:self];
-(无效)ShowLoginWindows:(id)发件人{
UIViewController*loginController=[[FHSTwitterEngine sharedEngine]loginControllerWithCompletionHandler:^(布尔成功){
NSLog(成功?@“L0L成功”:“不!!!洛根·费勒!!!”;
[[FHSTwitterEngine sharedEngine]loadAccessToken];
NSString*username=[FHSTwitterEngine sharedEngine].authenticatedUsername;
NSLog(@“用户名为:%@”,用户名);
如果(username.length>0){
[自我列表结果];
}
}];
[自我呈现视图控制器:loginController动画:是完成:无];
}
-(作废)清单结果{
NSString*username=[FHSTwitterEngine sharedEngine].authenticatedUsername;
NSMutableDictionary*dict1=[[FHSTwitterEngine sharedEngine]列表FriendsForser:用户名isID:否,光标:@“-1”];
//NSLog(@“===>%@,[dict1 objectForKey:@“users”];//这里是所有数据
NSMutableArray*数组=[dict1 objectForKey:@“用户”];
对于(int i=0;i
  • 参考runmad帖子上的评论,“[\uu NSArrayI objectAtIndex:]:索引0超出空数组的界限”的错误源是您尚未在模拟器中设置twitter帐户。您需要使用您的用户名和twitter提供的临时密码在twitter上签名

  • #import <Twitter/Twitter.h> -(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:NSJSONWritingPrettyPrinted 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); }]; }

  • UIButton *logIn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    logIn.frame = CGRectMake(100, 100, 100, 100);
    [logIn setTitle:@"Login" forState:UIControlStateNormal];
    [logIn addTarget:self action:@selector(showLoginWindow:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:logIn];
    
    [[FHSTwitterEngine sharedEngine]permanentlySetConsumerKey:@"Xg3ACDprWAH8loEPjMzRg" andSecret:@"9LwYDxw1iTc6D9ebHdrYCZrJP4lJhQv5uf4ueiPHvJ0"];
    [[FHSTwitterEngine sharedEngine]setDelegate:self];
    
    
     - (void)showLoginWindow:(id)sender {
    UIViewController *loginController = [[FHSTwitterEngine sharedEngine]loginControllerWithCompletionHandler:^(BOOL success) {
        NSLog(success?@"L0L success":@"O noes!!! Loggen faylur!!!");
        [[FHSTwitterEngine sharedEngine]loadAccessToken];
        NSString *username = [FHSTwitterEngine sharedEngine].authenticatedUsername;
        NSLog(@"user name is :%@",username);
        if (username.length > 0) {
            [self listResults];
        }
    }];
         [self presentViewController:loginController animated:YES completion:nil];
    }
     - (void)listResults {
    
    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"]);
    }
    }