很多时候,我们需要打开一个需要在UIViewController
SwiftUI 视图中呈现的视图。此类的突出示例UIDocumentInteractionController
和不太突出的示例是 SwiftyDropbox,用于在 Safari 中打开 Dropbox 的登录屏幕进行身份验证。常见的技术是在子类中创建一个UIViewController
按需的UIViewControllerRepresentable
并从中呈现视图控制器。但它从来没有对我有用。
struct DropboxView: UIViewControllerRepresentable {
typealias UIViewControllerType = UIViewController
@Binding var isShown : Bool
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
if isShown {
let scopeRequest = ScopeRequest(scopeType: .user, scopes: ["files.content.write"], includeGrantedScopes: false)
DropboxClientsManager.authorizeFromControllerV2(
UIApplication.shared,
controller: uiViewController,
loadingStatusDelegate: nil,
openURL: { (url: URL) -> Void in UIApplication.shared.open(url, options: [:], completionHandler: nil) },
scopeRequest: scopeRequest)
}
}
func makeUIViewController(context _: Self.Context) -> UIViewController {
return UIViewController()
}
}
当我使用修饰符从 SwiftUI 视图调用此类视图时,例如:
.fullScreenCover(isPresented: $showDropboxLoginView) {
DropboxView(isShown: $showDropboxLoginView)
}
我收到两个错误:
Cannot use Scene methods for URL, NSUserActivity, and other External Events without using SwiftUI Lifecycle. Without SwiftUI Lifecycle, advertising and handling External Events wastes resources, and will have unpredictable results.
和
Attempt to present <SwiftyDropbox.MobileSafariViewController: 0x107919600> on <UIViewController: 0x105a2f130> (from <UIViewController: 0x105a2f130>) whose view is not in the window hierarchy.
我想知道如何从 SwiftUI 视图中呈现此类视图控制器?
通过此设置,您不需要
fullScreenCover
只使用UIViewControllerRepresentable
.将其放在 中的任何位置
body
。fullScreenCover
和似乎都DropboxClientsManager
在使用 root。