Skip to content

Instantly share code, notes, and snippets.

@philipxjm
Created June 26, 2014 14:00
Show Gist options
  • Save philipxjm/ac83da94ea760d2e83a7 to your computer and use it in GitHub Desktop.
Save philipxjm/ac83da94ea760d2e83a7 to your computer and use it in GitHub Desktop.
-(void) logInWithTwitter
{
// check Twitter is configured in Settings or not
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]){
self.accountStore = [[ACAccountStore alloc] init]; //retain ACAccountStore
ACAccountType *twitterAcc = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[self.accountStore requestAccessToAccountsWithType:twitterAcc options:nil completion:^(BOOL granted, NSError *error){
//permission is granted
if (granted){
self.account = [[self.accountStore accountsWithAccountType:twitterAcc] lastObject];
NSDictionary* parameters = @{@"screen_name" : self.account.username};
NSURL* getUrl = [NSURL URLWithString:@"https://api.twitter.com/1.1/users/show.json"];
SLRequest *getRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:getUrl parameters:parameters];
getRequest.account=self.account;
[getRequest performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse, NSError *error){
//NSLog([NSString stringWithFormat:@"%li",(long)urlResponse.statusCode]);
NSError *jsonError;
NSDictionary *acquiredData = [NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingAllowFragments
error:&jsonError];
if(acquiredData){
self.userDict = @{
@"username":acquiredData[@"screen_name"],
@"profilePic":acquiredData[@"profile_image_url_https"],
@"name":acquiredData[@"name"],
@"id":acquiredData[@"id_str"],
@"location":acquiredData[@"location"]
};
/*
UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Log In Successful!" message:[NSString stringWithFormat:@"Welcome, %@", acquiredData[@"name"] ] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
[alert addButtonWithTitle:@"Thanks!"];
[alert show];
*/
[self performSegueWithIdentifier:@"Login" sender:self];
}else{
NSLog(@"JSON Error: %@", [jsonError localizedDescription]);
}
}];
}
//permission is not granted
else{
if (error == nil) {
NSLog(@"User Has disabled your app from settings...");
}
else{
NSLog(@"Error in Login: %@", error);
}
}
}];
}
else{
NSLog(@"Not Configured in Settings......"); // show user an alert view that Twitter is not configured in settings.
UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"No Set Up" message:@"Your device is not yet set up with Twitter, please go to System Prefence > Twitter to connect your device with Twitter" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
[alert show];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment