API iPhone- Twitter GET Users Abonnés / Suivant

Je veux être capable d'utiliser l'API Twitter pour ios 5 pour get tous les suiveurs d'users et le nom d'user suivant dans un NSDictionary

J'ai heurté un barrage routier. Je ne sais pas comment utiliser l'API Twitter le faire … Mais mon principal problème est d'get le nom d'user de l'user en premier lieu. Comment puis-je faire une request d'API pour find ces users lorsque je ne connais même pas le nom d'user?

Quelqu'un peut-il me donner un exemple sur comment get vos abonnés Twitter et suivre?

PS: J'ai déjà ajouté le cadre Twitter, et importé

C'est une combinaison de l'API Twitter d'Apple et de l'API de Twitter. C'est assez simple une fois que vous avez lu le code. Je vais fournir un exemple de code pour savoir comment get les 'amis' pour un count Twitter (c'est le terme pour les personnes qu'un user suit), ce qui devrait suffire à vous donner une méthode pour get les abonnés pour un count Twitter. Compte.

D'abord, ajoutez les frameworks Accounts et Twitter .

Maintenant, obtenons le count Twitter (s) présent sur l'appareil d'un user.

 #import <Accounts/Accounts.h> -(void)getTwitterAccounts { ACAccountStore *accountStore = [[ACAccountStore alloc] init]; // Create an account type that ensures Twitter accounts are resortingeved. 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.) } }]; } 

Maintenant, nous pouvons get les amis pour un count en utilisant la command GET friends / ids :

 #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 URLWithSsortingng:@"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); }]; } 

Quand vous voulez les abonnés pour un count, c'est presque pareil … Utilisez simplement l'URL http://api.twitter.com/1/followers/ids.format et transmettez les parameters nécessaires trouvés par GET followers / ids

J'espère que cela vous donnera une bonne longueur d'avance.

METTRE À JOUR:

Comme indiqué dans les commentaires, vous devriez utiliser l'appel API mis à jour: https://api.twitter.com/1.1/followers/list.json

  1. En se référant aux commentaires sur le post de runmad, la source d'erreur pour " [__NSArrayI objectAtIndex:]: index 0 beyond bounds pour le tableau vide" est que vous n'avez pas configuré le count twitter dans le simulateur. Vous devez signer twitter avec votre nom d'user et un mot de passe temporaire fourni par twitter.

  2. L'autre source d'erreur est "setObject pour l'erreur de key, l'ID de key est nul". Pour surmonter ce type ci-dessous le code: –

 -(void)getTwitterAccounts { ACAccountStore *accountStore = [[ACAccountStore alloc] init]; // Create an account type that ensures Twitter accounts are resortingeved. 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 URLWithSsortingng:@"https://api.twitter.com/1.1/followers/list.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]; NSLog(@"%@", twitterFriends); }]; } 

Utiliser FHSTwitterEngine

#import "FHSTwitterEngine.h"

Ajouter SystemConfiguration.framework

Écrivez le code suivant à votre viewDidLoad (pour la connection oauth)

 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]; NSSsortingng *username = [FHSTwitterEngine sharedEngine].authenticatedUsername; NSLog(@"user name is :%@",username); if (username.length > 0) { [self listResults]; } }]; [self presentViewController:loginController animated:YES completion:nil]; } - (void)listResults { NSSsortingng *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"]); } }