Created
June 21, 2017 08:18
-
-
Save randhirraj3130/8ad32fd87b862745d29c3222e24c89b5 to your computer and use it in GitHub Desktop.
twitter login in objective-c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//follow to following steps to get twitter login | |
step1- register your app on twitter dashboard | |
step 2- add following code in info.plist betbeen <dict> and </dict> | |
<key>CFBundleURLTypes</key> | |
<array> | |
<dict> | |
<key>CFBundleURLSchemes</key> | |
<array> | |
<string>twitterkit-<consumerKey></string> | |
</array> | |
</dict> | |
</array> | |
<key>LSApplicationQueriesSchemes</key> | |
<array> | |
<string>twitter</string> | |
<string>twitterauth</string> | |
</array> | |
step 3- download twitterkit and extract the zip file . freamwork file from twitterkit from the following url https://dev.twitter.com/twitterkit/ios/installation | |
step 4- add the following code in appdelegate .m | |
#import <TwitterKit/TwitterKit.h> | |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | |
{ | |
[[Twitter sharedInstance] startWithConsumerKey:@"" consumerSecret:@""]; | |
} | |
return YES; | |
} | |
and the call back url is | |
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url | |
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { | |
return [[Twitter sharedInstance] application:application openURL:url options:annotation]; | |
} | |
step 4- add the following code in the button action where you want | |
change the burtton type UIButton to TWTRLogInButton | |
step 5- add following function where the button action occured | |
-(void)twiterLogin{ | |
//twitterkit-N2wrNbNTIGaU2oIstKykH4pyS | |
[[NSUserDefaults standardUserDefaults]setValue:@"twitter" forKey:@"social"]; | |
[[Twitter sharedInstance]logInWithCompletion:^(TWTRSession * _Nullable session, NSError * _Nullable error) { | |
if(session){ | |
NSLog(@"twiterLogin function working"); | |
NSLog(@"signed in as %@", [session userName]); | |
TWTRAPIClient *client = [TWTRAPIClient clientWithCurrentUser]; | |
NSURLRequest *request = [client URLRequestWithMethod:@"GET" | |
URL:@"https://api.twitter.com/1.1/account/verify_credentials.json" | |
parameters:@{@"include_email": @"true", @"skip_status": @"true"} | |
error:nil]; | |
[client sendTwitterRequest:request completion:^(NSURLResponse *response, NSData *data, NSError *connectionError) { | |
// NSString* myString; | |
// myString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; | |
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL]; | |
NSLog(@" result %@",result); | |
NSString *user_name = [NSString stringWithFormat:@"%@",[result valueForKey:@"name"]]; | |
NSLog(@"twitter_name %@",user_name); | |
//screen_name | |
NSString *twitter_name = [NSString stringWithFormat:@"%@",[result valueForKey:@"screen_name"]]; | |
NSLog(@"twitter_name %@",twitter_name); | |
// [[NSUserDefaults standardUserDefaults]setValue:fb_name forKey:@"user_name"]; | |
NSString *twitter_id = [NSString stringWithFormat:@"%@",[result valueForKey:@"id"]]; | |
NSLog(@"twitter_id %@",twitter_id); | |
//[[NSUserDefaults standardUserDefaults]setValue:fb_id forKey:@"user_id"]; | |
NSString *twitter_email = [NSString stringWithFormat:@"%@",[result valueForKey:@"email"]]; | |
NSLog(@"twitter_email %@",twitter_email); | |
NSString *prifile_url = [NSString stringWithFormat:@"%@",[result valueForKey:@"profile_image_url"]]; | |
NSLog(@"prifile_url %@",prifile_url); | |
NSString*type =@"2"; | |
[[NSUserDefaults standardUserDefaults]setValue:@"twitter" forKey:@"social"]; | |
[self sendData:twitter_id :user_name :twitter_name :type]; | |
//[[NSUserDefaults standardUserDefaults]setValue:fb_name forKey:@"user_name"]; | |
}]; | |
}else{ | |
NSLog(@"twiterLogin function not working"); | |
UIAlertController * alert = [UIAlertController | |
alertControllerWithTitle:@"" | |
message:@"Set up your account in device to get the twitter login" | |
preferredStyle:UIAlertControllerStyleAlert]; | |
UIAlertAction* fbButton = [UIAlertAction | |
actionWithTitle:@"OK" | |
style:UIAlertActionStyleDefault | |
handler:^(UIAlertAction * action) | |
{ | |
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=TWITTER"]]; | |
[[self processHolderView]setHidden:YES]; | |
}]; | |
[alert addAction:fbButton]; | |
[self presentViewController:alert animated:YES completion:nil]; | |
} | |
if(error){ | |
NSLog(@"twiterLogin function not working %@",error.description); | |
} | |
}]; | |
} | |
step 6- click the project->info->url type->url scheme and add the following code | |
twitterkit-Consumer Key | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
twitter login by using swift 3.0 in Xcode 8