iOS 12-AppAuth重定向URL不会触发AppDelegate

时间:2019-05-28 20:43:56

标签: ios swift oauth-2.0 url-redirection appauth

我在代码上使用AppAuth

我设法成功进行了身份验证,但是当SFSafariViewController gets dismiss from my Controller时,重定向URL不会触发AppDelegate func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool

重定向URL是我的捆绑包标识符名称:BundleIdentifier:// authenticate

我在info.plist url方案中设置了具有相同名称的方案和url标识符。

当我在此函数上运行代码设置断点时,我可以看到我的重定向URL对standarizedURLstandarizedRedirectURL正确

- (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);

但是当AppAuth完成身份验证并且我有一个access token时,func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool不会被触发。

知道为什么吗?

这是我的代码

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)
    }

1 个答案:

答案 0 :(得分:0)

在iOS 12上,App-Auth使用ASWebAuthenticationSession,在iOS 11上,它使用现已弃用的SFAuthenticationSession,而不是要求应用程序支持手动处理重定向。为了支持iOS的早期版本,您仍然需要使用func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool方法中的代码。

作为参考,您可以在here的封面下查看AppAuth的操作。另外,this是一个很好的答案,它说明了如何在不使用AppAuth的情况下在iOS上一般获取OAuth令牌。