我正在使用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)
}
它只是等待并说“将你重定向到应用程序”但从未真正回到应用程序?
您需要将这两个函数添加到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