Stock photo is stock photo

iOS 11, Privacy and Single Sign On

Intuitive OAuth Flows Aren’t Gone — They’re Better

Jordan Morgan
Jul 14, 2017 · 6 min read

A quick note — all of my future posts will be published on my dedicated website and this publication is no longer being updated. Thanks for reading!


Wait but Why

Hi Auth Session, G’Day [Code Bloat, OAuth Libs, etc]

//Your main entry point
public init(url URL: URL, callbackURLScheme: String?, completionHandler: @escaping SFAuthenticationSession.CompletionHandler)
//Kick off auth request
open func start() -> Bool
//Cancel an in flight request
open func cancel()
//On completion, used in the designated initializer
public typealias CompletionHandler = (URL?, Error?) -> Swift.Void
//A trivial error that represents a cancellation
public struct SFAuthenticationError

The OAuth Dance

//Typedef block to handle response
let handler:SFAuthenticationSession.CompletionHandler = { (callBack:URL?, error:Error? ) in
guard error == nil, let successURL = callBack else {
return
}
let oauthToken = NSURLComponents(string: (successURL.absoluteString))?.queryItems?.filter({$0.name == "oauth_token"}).first
// Do what you now that you've got the token, or use the callBack URL
}
//OAuth Provider URL
let authURL = NSURL(string: "https://api.twitter.com/oauth/authenticate?oauth_token=amazingToken")
//Initialize auth session
authSession = SFAuthenticationSession(url: authURL, callbackURLScheme: nil, completionHandler: handler)
//Kick it off
authSession?.start()
authSession = SFAuthenticationSession(url: authURL, callbackURLScheme: nil, { (callBack:URL?, error:Error? ) in
//Handle auth
}

Closer Look

func authUser() 
{
let authSession = SFAuthenticationSession.....
authSession.start()
}
func authUser() 
{
//'self' is unnecessary, but illustrates it's an iVar
self.authSession = SFAuthenticationSession.....
self.authSession.start()
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool 
{
//Handle oauth response
}
//Use plist
authSession = SFAuthenticationSession(url: authURL, callbackURLScheme: nil, { (callBack:URL?, error:Error? ) in
//Handle auth
}
//Use callback defined at runtime
authSession = SFAuthenticationSession(url: authURL, callbackURLScheme: myCallbackURL, { (callBack:URL?, error:Error? ) in
//Handle auth
}
let didStart = authSession.start()
authSession = SFAuthenticationSession(... //Setup with initial provider
authSession.start() //Returns true
authSession = SFAuthenticationSession(... //Setup with another provider
authSession.start() //Returns false

Wrapping Up


The Traveled iOS Developer’s Guide

Cocoa Touch, unwrapped and analyzed by @JordanMorgan10.

Jordan Morgan

Written by

iOS @buffer. Author for Pluralsight, contributor to a few books, conference talks and some other stuff! Writing at swiftjectivec.com

The Traveled iOS Developer’s Guide

Cocoa Touch, unwrapped and analyzed by @JordanMorgan10. Updated biweekly.

Jordan Morgan

Written by

iOS @buffer. Author for Pluralsight, contributor to a few books, conference talks and some other stuff! Writing at swiftjectivec.com

The Traveled iOS Developer’s Guide

Cocoa Touch, unwrapped and analyzed by @JordanMorgan10. Updated biweekly.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store