[英]Attempting to load the view of a view controller while it is deallocating <SFAuthenticationViewController>
得到一個非常奇怪的錯誤,嘗試使用一些非常簡單的代碼。
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()
}
}
根據我的理解, 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>)
你認為創建這個auth流會更簡單,但顯然不是。
感謝任何意見,謝謝
您需要在頂層保留對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()
}
}
感謝這個中等帖子的修復: https : //medium.com/the-traveled-ios-developers-guide/ios-11-privacy-and-single-sign-on-6291687a2ccc我找不到對官方文檔中的范圍問題。
編輯: 官方文檔現在聲明“確保在會話進行時存在對SFAuthenticationSession實例的強引用”。 這似乎表明需要在會議范圍內推動會議。 這是由於SFAuthenticationSession初始化程序顯示同意對話框時發生的行為。
Edit2:SFAuthenticationSession在iOS 12中已棄用,並替換為ASWebAuthenticationSession。 但是,ASWebAuthenticationSession具有相同的范圍要求。 這篇博客對如何轉換有很好的描述。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.