【问题标题】:Callback in ios not returning for authenticationios中的回调未返回进行身份验证
【发布时间】:2017-12-30 08:14:10
【问题描述】:

我正在使用swifter 来获取访问令牌

swifter.authorize(with: URL(string: "callback://")!, presentFrom: self, success: { accessToken, response in
        print("HELLO")
        print(accessToken)
        // ...
    }, failure: { error in
        // ...
    })

我可以成功登录,因为应用程序打开了 SFSafariViewController,但是我没有被定向回我的应用程序,并且永远不会调用成功回调

这里是 swifter 的代码,我看到 presentFrom 应该是 SFSafariViewController,但是 SFSafariViewController 的 delgate 方法没有被触发

 public func authorize(with callbackURL: URL, presentFrom presentingViewController: UIViewController? , success: TokenSuccessHandler?, failure: FailureHandler? = nil) {
    self.postOAuthRequestToken(with: callbackURL, success: { token, response in
        var requestToken = token!
        NotificationCenter.default.addObserver(forName: .SwifterCallbackNotification, object: nil, queue: .main) { notification in
            NotificationCenter.default.removeObserver(self)
            presentingViewController?.presentedViewController?.dismiss(animated: true, completion: nil)
            let url = notification.userInfo![CallbackNotification.optionsURLKey] as! URL

            let parameters = url.query!.queryStringParameters
            requestToken.verifier = parameters["oauth_verifier"]

            self.postOAuthAccessToken(with: requestToken, success: { accessToken, response in
                self.client.credential = Credential(accessToken: accessToken!)
                success?(accessToken!, response)
                }, failure: failure)
        }

        let authorizeURL = URL(string: "oauth/authorize", relativeTo: TwitterURL.oauth.url)
        let queryURL = URL(string: authorizeURL!.absoluteString + "?oauth_token=\(token!.key)")!

        if #available(iOS 9.0, *) , let delegate = presentingViewController as? SFSafariViewControllerDelegate {
            let safariView = SFSafariViewController(url: queryURL)
            safariView.delegate = delegate
            presentingViewController?.present(safariView, animated: true, completion: nil)
        } else {
            UIApplication.shared.openURL(queryURL)
        }
    }, failure: failure)
}

【问题讨论】:

  • 您找到解决方案了吗?我现在也在为此苦苦挣扎

标签: ios swift twitter


【解决方案1】:

它是否只是等待并说“将您重定向回应用程序”但实际上从未返回应用程序?

您需要将这两个函数都添加到 Appdelegate

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    Swifter.handleOpenURL(url)
    return true
}

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    Swifter.handleOpenURL(url as URL)

    return true
}

也可以试试看这个以获得进一步的帮助:How to Authorize Twitter with Swifter

【讨论】:

    猜你喜欢
    • 2020-12-17
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    • 2021-01-01
    • 2014-07-07
    • 2015-09-23
    • 2013-06-17
    • 2018-01-05
    相关资源
    最近更新 更多