Skip to content

Instantly share code, notes, and snippets.

@eito
Created February 20, 2019 23:47
Show Gist options
  • Save eito/c1dcea85b5bcd7c6a90afca342989f15 to your computer and use it in GitHub Desktop.
Save eito/c1dcea85b5bcd7c6a90afca342989f15 to your computer and use it in GitHub Desktop.
iOS Authentication Session for iOS 11/12
import Foundation
import SafariServices
import AuthenticationServices
private protocol AuthenticationSessionProvider {
func start() -> Bool
func cancel()
}
extension SFAuthenticationSession: AuthenticationSessionProvider {}
@available(iOS 12.0, *)
extension ASWebAuthenticationSession: AuthenticationSessionProvider {}
class AuthenticationSession: NSObject {
private var session: AuthenticationSessionProvider?
private let completion: ((URL?, Error?) -> Void)
init(url: URL, callbackSchemeURL: String?, completion: @escaping (URL?, Error?) -> Void) {
if #available(iOS 12.0, *) {
session = ASWebAuthenticationSession(url: url, callbackURLScheme: callbackSchemeURL, completionHandler: completion)
} else {
session = SFAuthenticationSession(url: url, callbackURLScheme: callbackSchemeURL, completionHandler: completion)
}
self.completion = completion
}
func start() -> Bool {
return session?.start() ?? false
}
func cancel() {
session?.cancel()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment