Skip to content

Instantly share code, notes, and snippets.

@WilliamDenniss
Last active March 24, 2020 14:06
Show Gist options
  • Save WilliamDenniss/70da59c634055dfefbb79d2df14f306f to your computer and use it in GitHub Desktop.
Save WilliamDenniss/70da59c634055dfefbb79d2df14f306f to your computer and use it in GitHub Desktop.
AppAuth for iOS with Custom Browser

Custom Browser support for AppAuth for iOS has been implemented. Here's how to configure AppAuth to use a custom browser:

First, add the following array to your Info.plist (in XCode, right click -> Open As -> Source Code)

	<key>LSApplicationQueriesSchemes</key>
	<array>
		<string>googlechromes</string>
		<string>opera-https</string>
		<string>firefox</string>
	</array>

This is required so that AppAuth can test for the browser and open the app store if it's not installed. You only need to include the URL scheme of the browser you intend to use.

Then tweak your call to authStateByPresentingAuthorizationRequest

https://github.com/openid/AppAuth-iOS/blob/135f99d2cb4e9d18d310ac2588b905e612461561/Examples/Example-iOS_ObjC/Source/AppAuthExampleViewController.m#L215-L218

Change it to:

  OIDAuthorizationUICoordinatorCustomBrowser *coordinator =
      [OIDAuthorizationUICoordinatorCustomBrowser CustomBrowserChrome];
  appDelegate.currentAuthorizationFlow =
   [OIDAuthState authStateByPresentingAuthorizationRequest:request
                                           UICoordinator:coordinator
                                                callback:^(OIDAuthState *_Nullable authState, NSError *_Nullable error) 

As you can see, with the latter code block we are creating the UI coordinator manually and using it, rather than using the default. This technique can be used with the automatic code exchange of OIDAuthState (as shown above), and manually with OIDAuthorizationService:presentAuthorizationRequest:UICoordinator:callback:.

That's it! With those two changes (which you can try on the included sample), AppAuth will use Chrome iOS for the authorization request (and open Chrome in the App Store if it's not installed).

⚠️Note: this feature is not intended for consumer apps. It is designed for advanced enterprise use-cases where the app developers have greater control over the operating environment and have special requirements that require a custom browser like Chrome.

Chrome, Firefox, Opera Mini and Safari are supported out of the box, however it's quite simple to support more browsers without changes to AppAuth, by following the patterns used for the included browsers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment