- (IBAction)getTwitterContact:(id)sender
{
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 options:nil completion:^(BOOL granted, NSError *error)
//[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:parametersrequestMethod:TWRequestMethodGET];
SLRequest *aRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodPOST
URL:followingURL
parameters:parameters];
// 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
[aRequest setAccount:account];
// Perform the request for Twitter friends
[aRequest 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);
}];
}
我已经使用此代码来获取Twitter粉丝,但崩溃我的应用程序 错误日志是
由于未捕获的异常“NSRangeException”而终止应用程序,原因:'* - [__ NSArrayI objectAtIndex:]:索引0超出空数组的边界'**
myarray是零,这就是为什么这个错误来了,但我不知道。为什么这是nill ..
我可能会出错帮助我这样做。
提前感谢..
答案 0 :(得分:0)
嘿,您正在检查多个帐户,但是您没有检查是否至少有一个帐户。
所以,代替下面的代码:
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]];
}
试试这个:
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 if([accountsArray count] == 1) {
[self getTwitterFriendsForAccount:[accountsArray objectAtIndex:0]];
} else {
// There is no account setup in settings.
// Show message to go settings and setup an account for twitter.
}
您还应该尝试使用Twitter-API 1.1而不是API-1.0,因为现在不推荐使用API 1.0。
所以请使用:https://api.twitter.com/1.1/followers/ids.json
取代:https://api.twitter.com/1/followers/ids.json
// Prepare to fetch available Twitter accounts in the device settings
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
已更新:以下是适用于我的代码。
// Fetch accounts available locally
[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) {
} else {
}
} else {
// Permission Denied
}
}];