gpt4 book ai didi

ios - 调用文件选择器时,WKWebview 具有新的 iOS13 模式崩溃

转载 作者:行者123 更新时间:2023-12-01 23:51:07 27 4
gpt4 key购买 nike

我在 iOS13 上的模态视图 Controller 中有一个 webview。当用户尝试将图像上传到 WebView 时,它会崩溃。

这是我遇到的异常:

2019-09-30 17:50:10.676940+0900 Engage[988:157733] * Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIDocumentMenuViewController (). In its current trait environment, the modalPresentationStyle of a UIDocumentMenuViewController with this style is UIModalPresentationPopover. You must provide location information for this popover through the view controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the view controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.' * First throw call stack: (0x18926c98c 0x188f950a4 0x18cb898a8 0x18cb939b4 0x18cb914f8 0x18d283b98 0x18d2737c0 0x18d2a3594 0x1891e9c48 0x1891e4b34 0x1891e5100 0x1891e48bc 0x193050328 0x18d27a6d4 0x1002e6de4 0x18906f460) libc++abi.dylib: terminating with uncaught exception of type NSException

我不确定在哪里可以设置这个委托(delegate)...

我做了一个示例项目:https://github.com/ntnmrndn/WKUploadFormCrash并向Apple填写了错误报告

最佳答案

正如 @jshapy8 正确指出的那样,您需要重写 present() 方法并设置 .sourceView/.sourceFrame/.barButtonItem 手动。
但您需要记住,如果保存 WkWebViewUIViewControllerUINavigationController 呈现,则 UINavigationController code> 负责呈现其他 UIViewController



除非您使用的是 iPad。



因此,实际上您需要重写 UINavigationController 以及保存 UIViewController 中的 present() 方法>WkWebView.



在下面的示例中,保存 WkWebViewUIViewController 称为 WebVC



在您的UINavigationController中您需要添加:



  override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
if let webVC = viewControllers.filter({ $0 is WebVC }).first as? WebVC {
webVC.setUIDocumentMenuViewControllerSoureViewsIfNeeded(viewControllerToPresent)
}
super.present(viewControllerToPresent, animated: flag, completion: completion)
}

并且在您的WebVC中您需要添加:

  override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
setUIDocumentMenuViewControllerSoureViewsIfNeeded(viewControllerToPresent)
super.present(viewControllerToPresent, animated: flag, completion: completion)
}

func setUIDocumentMenuViewControllerSoureViewsIfNeeded(_ viewControllerToPresent: UIViewController) {
if #available(iOS 13, *), viewControllerToPresent is UIDocumentMenuViewController && UIDevice.current.userInterfaceIdiom == .phone {
// Prevent the app from crashing if the WKWebView decides to present a UIDocumentMenuViewController while it self is presented modally.
viewControllerToPresent.popoverPresentationController?.sourceView = webView
viewControllerToPresent.popoverPresentationController?.sourceRect = CGRect(x: webView.center.x, y: webView.center.y, width: 1, height: 1)
}
}

因此您可以使用新的 iOS 13 模态演示样式并上传文件而不会崩溃 😃

编辑:这种崩溃行为似乎是(另一个)iOS 13 bug,因为这只是 iPhone 上的问题,而不是 iPad 上的问题(刚刚在装有 iOS 12 和 13 的 iPad 上进行了测试。看起来苹果工程师只是忘记了,如果 WKWebView 以新的模式呈现样式呈现,则 UIDocumentMenuViewControllerUIModalPresentationPopover 呈现风格,甚至在手机上也是如此,直到 iOS 13 才出现这种情况。

我更新了我的代码,所以现在它仅针对手机类型设置 .sourceView/.sourceFrame/.barButtonItem ,因为平板电脑类型会由 iOS 自行正确处理。

关于ios - 调用文件选择器时,WKWebview 具有新的 iOS13 模式崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58164583/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com