Twitter OAuth Login with Safari View Controller on iOS
TwitterSafariViewControllerAuth
adds Twitter login using Apple's SFSafariViewController
, which is safe for your users WKWebView
💯 😄UIWebView
Bonus: access saved credentials in Safari or use 1Password like a boss.
platform :ios, '9.0'
pod 'TwitterSafariViewControllerAuth'
Add the TwitterSafariViewControllerAuth/
folder to your project.
A Twitter app with a valid Callback URL
is required: the value entered there is not used but if the field is empty, redirects are disallowed.
A URL scheme for your iOS app is required. In Project Settings → Info: scroll down to URL Types and add an entry with +
- Identifier: your-app-bundle-id, i.e.
com.dkhamsing.TwitterAuthDemo
- URL Schemes: url-scheme, i.e.
TwitterAuthDemo
(if there is only one scheme, this value is read and set as the URL scheme forTwitterSafariViewControllerAuth
)
// MyViewController.m
static NSString *const kConsumerKey = @"<YOUR APP CONSUMER KEY>";
static NSString *const kConsumerSecret = @"<YOUR APP CONSUMER SECRET>";
#import "TwitterSafariViewControllerAuth.h"
// ...
// URL Scheme is assumed to be first scheme in bundle plist, to set this manually, use - configureConsumerKey:clientSecret:urlScheme:.
[[TwitterSafariViewControllerAuth sharedInstance] configureConsumerKey:kConsumerKey clientSecret:kConsumerSecret];
[[TwitterSafariViewControllerAuth sharedInstance] presentOAuthLoginFromController:self];
// AppDelegate.m
#import "TwitterSafariViewControllerAuth.h"
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
[[TwitterSafariViewControllerAuth sharedInstance] handleOpenUrl:url options:options success:^(NSDictionary *results) {
NSLog(@"Results: %@", results);
/*
Results: {
data = <...>;
"data_string" = "..."
"oauth_token" = "...";
"oauth_token_secret" = "...";
"screen_name" = dkhamsing;
"user_id" = ...;
"x_auth_expires" = 0;
}
*/
} failure:nil];
// ...
}
A demo project is included.
Based on FHSTwitterEngine
.
This project is available under the MIT license. See the LICENSE file for more info.