我试图消除xcode控制台中一个恼人的警告/错误。我已经实现了一个使用ASWebAuthenticationSession打开密钥披风的自定义插件,并且我在解决如何调用主线程窗口时遇到了问题。
这是代码:
@available(iOS 13.0, *)
@objc(KeycloakPlugin)
public class KeycloakPlugin: CAPPlugin, ObservableObject, ASWebAuthenticationPresentationContextProviding {
var webAuthSession: ASWebAuthenticationSession?
public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
return self.bridge?.webView?.window ?? ASPresentationAnchor()
}当我打开用于身份验证的外部url时,这一行表示如下:
return self.bridge?.webView?.window ?? ASPresentationAnchor()
在这种情况下,我得到:
必须仅从主线程使用
UIView.window。
你知道怎么解决这个问题吗?
发布于 2022-07-28 09:59:31
也许这会有帮助:
https://capacitorjs.com/docs/core-apis/ios
如果您将代码包装在DispatchQueue.main.async中,它应该会删除警告。它还与DispatchQueue.main.sync一起工作,具体取决于实现。
就像这样:
@objc(MyPlugin)
public class MyPlugin: CAPPlugin, ASWebAuthenticationPresentationContextProviding {
@objc func myPluginMethod(_ call: CAPPluginCall) {
// do something here
}
public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
var view: ASPresentationAnchor?
DispatchQueue.main.sync {
view = self.bridge?.webView?.window
// or use async and do something here, e.g. create an implementation instance and pass the view
}
return view ?? ASPresentationAnchor()
}
}https://stackoverflow.com/questions/73119842
复制相似问题