4

My mobile app (React Native) uses a Web View (Chrome Custom Tabs on Android and Safari View Controller on iOS) for a small user flow within the app.

At the end of the process the web view should close and return to the app. I've managed to make it work on Android by using a custom scheme URL redirect at the end (myapp://home), but that doesn't seem to work on iOS. In fact, I haven't found a way to do it on iOS at all.

What approach is generally used for this type of scenario?

Flavien
  • 7,497
  • 10
  • 45
  • 52

1 Answers1

0

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()
Ankit Deshmukh
  • 549
  • 5
  • 11