1

我正在使用AppAuth我的代码。

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

重定向 URL 是我的 Bundle Identifier 名称:BundleIdentifier://authenticate

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

当我运行我的代码在这个函数上设置一个断点时,我可以看到我的重定向 url 正确,standarizedURL并且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);

但是当 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)
    }
4

1 回答 1

1

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

作为参考,您可以在此处查看 AppAuth 在幕后所做的事情。此外,是一个很好的答案,它解释了如何在不使用 AppAuth 的情况下在 iOS 上一般获取 OAuth 令牌。

于 2019-06-24T18:10:20.483 回答