Sign in with Twitter using Swift and OAuth in ios

I am very new to Swift and iOS. I want to implement twitter login using OAuth in my fast iOS app.

I implemented it, but I got it

[{"message": "Failed to authenticate you", "code": 32}]}

Mistake

+9
ios twitter-oauth swift
source share
4 answers

You can use the Social Framework to login.

And I used this one for this.

+5
source share

I do not know how you do it. But it can be useful :)

+1
source share

Yes, it’s pretty difficult to implement Twitter OAuth on iOS. In my applications, I use ACAccountStore for twitter authentication. For you, I can recommend using this library.

0
source share

Too easy

I used the STTwitter framework, which is very nice. also watch this video Only the Twitter application Only Authentication and check out the demo of the STTwitterDemoiOS demo.

Step 1: Create a Twitter app and get the consumer key and consumer secrets.

Step 2: Download the STTwitter Framework and drag the file into the Xcode project.

Step 3: Login to UIWebView / Safari

 - (IBAction)signInWithTwitterClicked:(id)sender { //login by website self.twitter = [STTwitterAPI twitterAPIWithOAuthConsumerKey:CONSUMER_KEY consumerSecret:CONSUMER_SECRETE]; [_twitter postTokenRequest:^(NSURL *url, NSString *oauthToken) { NSLog(@"URL: %@", url); NSLog(@"OauthToken: %@", oauthToken); // if(1) { // [[UIApplication sharedApplication] openURL:url]; //} else { //WebViewVc taken from STTwitterDemoiOS demo. WebViewVC *webViewVC = [self.storyboard instantiateViewControllerWithIdentifier:@"WebViewVC"]; [self presentViewController:webViewVC animated:YES completion:^{ NSURLRequest *request = [NSURLRequest requestWithURL:url]; [webViewVC.webView loadRequest:request]; }]; // } } authenticateInsteadOfAuthorize:NO forceLogin:@(YES) screenName:nil oauthCallback:@"myapp://twitter_access_tokens/" errorBlock:^(NSError *error) { NSLog(@"-- error: %@", error); // _loginStatusLabel.text = [error localizedDescription]; }]; } 

// Step 4 and step 5 imp for a callback to our application

Step 4: Configure info.plist as shown in the image. enter image description here

Step 5: Processing the Application Delegation Method

  - (NSDictionary *)parametersDictionaryFromQueryString:(NSString *)queryString { NSMutableDictionary *md = [NSMutableDictionary dictionary]; NSArray *queryComponents = [queryString componentsSeparatedByString:@"&"]; for(NSString *s in queryComponents) { NSArray *pair = [s componentsSeparatedByString:@"="]; if([pair count] != 2) continue; NSString *key = pair[0]; NSString *value = pair[1]; md[key] = value; } return md; } - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { //Twitter integration // if ([[url scheme] isEqualToString:@"myapp"] == NO) return NO; NSDictionary *d = [self parametersDictionaryFromQueryString:[url query]]; NSString *token = d[@"oauth_token"]; NSString *verifier = d[@"oauth_verifier"]; // NSLog(@"Twitter Token=> %@\n Twitter Verifier=>%@",token,verifier); ViewController *vc = (ViewController *)[[self window] rootViewController]; StartupViewController *startVc=(StartupViewController *)[[vc childViewControllers] objectAtIndex:0]; [startVc setOAuthToken:token oauthVerifier:verifier]; //startVc is my controller where my "Login with twitter" button is there. //if no Facebook integration then return YES instead if return //[FBAppCall handleOpenURL:url sourceApplication:sourceApplication]; //Facebook Integration return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication]; } 

Step 6: Get user credentials.

 -(void)setOAuthToken:(NSString *)token oauthVerifier:(NSString *)verifier { // in case the user has just authenticated through WebViewVC [self dismissViewControllerAnimated:YES completion:^{ //Dismiss presented controller. }]; [_twitter postAccessTokenRequestWithPIN:verifier successBlock:^(NSString *oauthToken, NSString *oauthTokenSecret, NSString *userID, NSString *screenName) { //Here is your Ans. NSLog(@"SUCCESS screenName: %@ ,userID=%@", screenName,userID); } errorBlock:^(NSError *error) { NSLog(@"-- %@", [error localizedDescription]); }]; } 

hope this helps someone.

-one
source share

All Articles