Created
September 15, 2015 21:15
-
-
Save rsattar/59c710880d897fb75fda to your computer and use it in GitHub Desktop.
SFSafariViewController autologin snippets
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
// Say this is in your app delegate | |
- (void) attemptAutologin | |
{ | |
NSURL *autologinURL = ...some..url..that..does..autologin..on..your..server | |
self.safariViewController = [[SFSafariViewController alloc] initWithURL:autologinURL]; | |
self.safariViewController.modalPresentationStyle = UIModalPresentationOverFullScreen; | |
self.safariViewController.view.alpha = 0.0; | |
[self.window.rootViewController presentViewController:self.safariViewController | |
animated:NO | |
completion:nil]; | |
self.awaitingAutologinResponse = YES; | |
} | |
// Now, the page at 'autloginURL' will be written to check its | |
// cookies to see if the user is logged in, and then somehow your | |
// server will generate a one-time "token" that your iOS app can | |
// use to login as the same user that is logged in to the web. | |
// | |
// Then, the page will redirect back to your app (using a url handler | |
// that your app registered, or BETTER YET, a url that is registered | |
// using the apple-app-site-associations, so other apps can't "hijack" | |
// the same url your app wants to use and potentially get to the | |
// autologin token. | |
// | |
// Note that all of the above happens while SFSafariViewController is | |
// still being "presented" (but hidden), so the app doesn't actually | |
// switch over to the Safari app. | |
- (BOOL) application:(UIApplication *)application | |
openURL:(NSURL *)url | |
sourceApplication:(NSString *)sourceApplication | |
annotation:(id)annotation | |
{ | |
NSDictionary *params = url.queryDictionary; | |
// Your server might return it differently, but this is | |
self.autologinAuthToken = params[@"autologin_token"]; | |
if (self.safariViewController != nil) { | |
[self.window.rootViewController dismissViewControllerAnimated:NO completion:^{ | |
if (self.awaitingAutologinResponse) { | |
if (self.autologinAuthToken.length > 0) { | |
// Save this url for later, and try and authenticate first | |
// The url might have more information than just the autologin_token | |
self.pendingURLToOpen = url; | |
[self autologinWithClusterToken:self.autologinAuthToken]; | |
} else { | |
[self finishAutologinAttemptWithSuccess:NO error:nil]; | |
} | |
} | |
}]; | |
return YES; | |
} | |
// Handle other actual open url cases | |
return NO; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment