Skip to content

Instantly share code, notes, and snippets.

@aug2uag
Created January 11, 2014 23:41
Show Gist options
  • Select an option

  • Save aug2uag/8378475 to your computer and use it in GitHub Desktop.

Select an option

Save aug2uag/8378475 to your computer and use it in GitHub Desktop.
ACAccountStore Twitter
ACAccountStore *store = [[ACAccountStore alloc] init]; // Long-lived
ACAccountType *twitterType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[store requestAccessToAccountsWithType:twitterType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted) {
// Access has been granted, now we can access the accounts
// Remember that twitterType was instantiated above
NSArray *twitterAccounts = [store accountsWithAccountType:twitterType];
// If there are no accounts, we need to pop up an alert
if(twitterAccounts != nil && [twitterAccounts count] == 0) {
[[[UIAlertView alloc] initWithTitle:@"No Twitter Accounts"
message:@"There are no Twitter accounts configured. You can add or create a Twitter account in Settings."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
} else {
ACAccount *account = [twitterAccounts objectAtIndex:0];
// Do something with their Twitter account
NSLog(@"acaccount = %@", account);
NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/account/verify_credentials.json"];
TWRequest *req = [[TWRequest alloc] initWithURL:url
parameters:nil
requestMethod:TWRequestMethodGET];
// Important: attach the user's Twitter ACAccount object to the request
req.account = account;
[req performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse,
NSError *error) {
// If there was an error making the request, display a message to the user
if(error != nil) {
[[[UIAlertView alloc] initWithTitle:@"Twitter Error"
message:@"There was an error talking to Twitter. Please try again later."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
return;
}
// Parse the JSON response
NSError *jsonError = nil;
id resp = [NSJSONSerialization JSONObjectWithData:responseData
options:0
error:&jsonError];
// If there was an error decoding the JSON, display a message to the user
if(jsonError != nil) {
[[[UIAlertView alloc] initWithTitle:@"Twitter Error"
message:@"Twitter is not acting properly right now. Please try again later."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
return;
}
NSLog(@"resp = %@", resp);
NSString *screenName = [resp objectForKey:@"screen_name"];
NSString *fullName = [resp objectForKey:@"name"];
NSString *location = [resp objectForKey:@"location"];
// Make sure to perform our operation back on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
// Do something with the fetched data
NSLog(@"screenName = %@\nfullName = %@\nlocation = %@", screenName, fullName, location);
});
}];
}
}
// Handle any error state here as you wish
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment