P S
P S

Reputation: 567

iOS 12 - AppAuth redirect URL not trigger AppDelegate

I am using AppAuth on my code.

I manage to authenticate successful , but when the SFSafariViewController gets dismiss from my Controller , the redirect url does not trigger the AppDelegate func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool

The redirect URL is my Bundle Identifier name : BundleIdentifier://authenticate

I have setup in info.plist url Schemes and url identifier which they have the same name.

When I run my code setting a break point on this func I can see my redirect url correct for standarizedURL and standarizedRedirectURL

- (BOOL)shouldHandleURL:(NSURL *)URL {
  NSURL *standardizedURL = [URL standardizedURL];
  NSURL *standardizedRedirectURL = [_request.redirectURL standardizedURL];

    return OIDIsEqualIncludingNil(standardizedURL.scheme, standardizedRedirectURL.scheme) &&
    OIDIsEqualIncludingNil(standardizedURL.user, standardizedRedirectURL.user) &&
    OIDIsEqualIncludingNil(standardizedURL.password, standardizedRedirectURL.password) &&
    OIDIsEqualIncludingNil(standardizedURL.host, standardizedRedirectURL.host) &&
    OIDIsEqualIncludingNil(standardizedURL.port, standardizedRedirectURL.port) &&
    OIDIsEqualIncludingNil(standardizedURL.path, standardizedRedirectURL.path);

But when AppAuth finishes the authentication and I have an access token , func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool doesn't get triggered.

Any idea why?

Here is my code

class func signInAuth(discoveryURLstr: String,presenter : UIViewController,completionHandler: @escaping ( (OIDAuthState?,Error?) -> () )){

        guard let discoveruURL = URL(string: discoveryURLstr) else{
            completionHandler(nil,AuthErrors.InvalidDiscoveryURL)
            return
        }

        appAuthDiscoverConfiguration(discoveryURL: discoveruURL) { (configurationFile, error) in

            guard let configurationFile = configurationFile else {
                completionHandler(nil,AuthErrors.InvalidConfigurationFile)
                return
            }

            let authRequest = appAuthRequest(configurationFile: configurationFile)

             self.appAuthenticationSession = OIDAuthState.authState(byPresenting: authRequest, presenting: presenter, callback: { (state, error) in

                if let error = error {
                    //self.authState = nil
                    completionHandler(nil,error)
                    return
                }

                if let state = state {
                    self.authState = state
                    completionHandler(state,nil)

                }else{
                    completionHandler(nil,AuthErrors.InvalideState)
                }
            })

        }


    }

    class func appAuthDiscoverConfiguration(discoveryURL : URL, completionHandler: @escaping ((OIDServiceConfiguration?,Error?) -> ())) {

        OIDAuthorizationService.discoverConfiguration(forDiscoveryURL: discoveryURL) { (configuration, error) in

            if let error = error {
                completionHandler(nil,error)
                return
            }else{
                guard let configurationFile = configuration else {
                    completionHandler(nil,AuthErrors.InvalidConfigurationFile)
                    return
                }
                completionHandler(configurationFile,nil)
            }

        }

    }

    class func appAuthRequest(configurationFile : OIDServiceConfiguration) -> OIDAuthorizationRequest{

        return OIDAuthorizationRequest(configuration: configurationFile, clientId: AppAuthConstants.clientId, clientSecret: nil, scope: AppAuthConstants.scope, redirectURL: AppAuthConstants.redirectURL, responseType: AppAuthConstants.responseType, state: nil, nonce: nil, codeVerifier: nil, codeChallenge: nil, codeChallengeMethod: nil, additionalParameters: AppAuthConstants.additionalParameters)
    }

Upvotes: 1

Views: 2073

Answers (1)

HoppedUpDev
HoppedUpDev

Reputation: 71

On iOS 12, App-Auth uses ASWebAuthenticationSession, and on iOS 11, it uses the now-deprecated SFAuthenticationSession instead of requiring the app to support handling the redirect manually. To support earlier versions of iOS, you still need your code in the func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool method.

For reference, you can see what AppAuth is doing under the covers here. Also, this is a great answer that explains how to generically get an OAuth token on iOS without using AppAuth.

Upvotes: 1

Related Questions