Created
February 20, 2019 23:47
-
-
Save eito/c1dcea85b5bcd7c6a90afca342989f15 to your computer and use it in GitHub Desktop.
iOS Authentication Session for iOS 11/12
This file contains hidden or 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
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