Skip to content

Instantly share code, notes, and snippets.

@mafellows
Created May 13, 2015 02:08
Show Gist options
  • Save mafellows/65de799315c053a464f4 to your computer and use it in GitHub Desktop.
Save mafellows/65de799315c053a464f4 to your computer and use it in GitHub Desktop.
- (void)logInWithTwitter
{
[PFTwitterUtils logInWithBlock:^(PFUser *user, NSError *error) {
if (error) {
NSLog(@"Failed to login with Twitter..");
} else if (user.isNew) {
// Get information for new user
NSURL *verify = [NSURL URLWithString:@"https://api.twitter.com/1.1/account/verify_credentials.json"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:verify];
[[PFTwitterUtils twitter] signRequest:request];
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
// Use the response in the JSON object to set user info as necessary.. Example:
NSString *name = [jsonArray valueForKey:@"name"];
[user setObject:name forKey:@"name"];
[user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *saveError) {
if (saveError) {
NSLog(@"Failed to save new user.");
} else {
NSLog(@"Successfully signed in new user.");
}
}];
} else if (user) {
NSLog(@"Successfully signed in.");
}
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment