For iOS 11 and above, you can use SFAuthenticationSession.
Example:
let url = URL(string: "https://www.example.com")
let callbackURLScheme = "myapp"
let webSession = SFAuthenticationSession(url: url, callbackURLScheme: callbackURLScheme, completionHandler: { (url, error) in
//Will be triggered when a URL of scheme 'myapp' is launched
//or if the user clicks on cancel button in SFAuthenticationSession window (error case)
})
webSession.start()
Note: This is deprecated by apple
For iOS 12 and above, use ASWebAuthenticationSession. Example:
let url = URL(string: "https://www.example.com")
let callbackURLScheme = "myapp"
let webAuthSession = ASWebAuthenticationSession(url: url, callbackURLScheme: callbackURLScheme, completionHandler: { (url, error) in
//Will be triggered when a URL of scheme 'myapp' is launched
//or if the user clicks on cancel button in SFAuthenticationSession window (error case)
})
//available only in iOS 13 and above (reference: https://developer.apple.com/documentation/authenticationservices/aswebauthenticationsession/3237232-presentationcontextprovider)
webAuthSession.presentationContextProvider = presentationContextProvider // example: UIViewController
webAuthSession.start()