Wil*_*ill 4 ios oauth-2.0 swift
尝试使用一些非常简单的代码,得到了一个奇怪的错误。
import SafariService
class ViewController: UIViewController{
///All my stuff
@IBAction func connectToReddit(){
let authURL = URL(string: "https://www.reddit.com/api/v1/authorize?client_id=myID&response_type=code&state=myState&redirect_uri=myRedirectURI&duration=permanent&scope=myscopes")
let scheme = "redditwatch://"
let authSession = SFAuthenticationSession(url: authURL!, callbackURLScheme: scheme, completionHandler: { (url, error) in
print(url?.absoluteString)
print(error?.localizedDescription)
})
authSession.start()
}
}
Run Code Online (Sandbox Code Playgroud)
据我了解,authSession.start()向用户提供了UIAlertController,它确实可以为我的代码提供服务,但是控制器在此后消失了,并显示了错误
[Warning] Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<SFAuthenticationViewController: 0x7fc1c201f600>)
Run Code Online (Sandbox Code Playgroud)
您可能会认为创建此身份验证流会更简单,但显然并非如此。
感谢任何输入,谢谢
您需要在顶级保留对SFAuthenticationSession的引用。这应该可以解决问题:
import SafariService
class ViewController: UIViewController{
var authSession: SFAuthenticationSession?
///All my stuff
@IBAction func connectToReddit(){
let authURL = URL(string: "https://www.reddit.com/api/v1/authorize?client_id=myID&response_type=code&state=myState&redirect_uri=myRedirectURI&duration=permanent&scope=myscopes")
let scheme = "redditwatch://"
authSession = SFAuthenticationSession(url: authURL!, callbackURLScheme: scheme, completionHandler: { (url, error) in
print(url?.absoluteString)
print(error?.localizedDescription)
})
authSession?.start()
}
}
Run Code Online (Sandbox Code Playgroud)
将此修复归功于此中级帖子:https : //medium.com/the-traveled-ios-developers-guide/ios-11-privacy-and-single-sign-on-6291687a2ccc 我找不到对官方文档中的范围问题。
编辑:官方文档现在声明“确保在会话进行过程中对SFAuthenticationSession实例有强烈的引用。” 这似乎表明需要将会话范围扩大。这是由于SFAuthenticationSession初始化程序显示同意对话框时发生的行为。
Edit2:SFAuthenticationSession在iOS 12中已弃用,并被ASWebAuthenticationSession取代。但是,ASWebAuthenticationSession具有相同的范围要求。该博客很好地描述了如何进行转换。
| 归档时间: |
|
| 查看次数: |
720 次 |
| 最近记录: |