调用文件选取器时,带有新iOS13模式崩溃的WKWebview [英] WKWebview with the new iOS13 modal crash when a file picker is invoked
问题描述
我在iOS13上的模式视图控制器中有一个Webview。 当用户尝试将图像上载到Webview时,它会崩溃。
这是我收到的异常:
2019-09-30 17:50:10.676940+0900Engage[988:157733]*终止APP 由于未捕获异常‘NSGenericException’,原因:‘您的 应用程序已呈现UIDocumentMenuView控制器 ()。以其目前的特点 环境的modalPresentationStyle 具有此样式的UIDocumentMenuViewController是 UIModalPresentationPopover.您必须提供以下项目的位置信息 此弹出窗口通过视图控制器的 PopoverPresentationControl.您必须提供SourceView 和SourceRect或barButtonItem。如果此信息未知 当您呈现视图控制器时,可以在 UIPopoverPresentationControllerDelegate方法 -preparareForPopoverPresentation‘ *第一次抛出调用堆栈:(0x18926c98c 0x188f950a4 0x18cb898a8 0x18cb939b4 0x18cb914f8 0x18d283b98 0x18d2737c0 0x18d2a3594 0x1891e9c48 0x1891e4b34 0x1891e5100 0x1891e48bc 0x193050328 0x18d27a6d4 0x1002e6de4 0x18906f460)libc++abi.dylib:终止 未捕获类型为NSException的异常
我不确定在哪里可以设置此委派...
我做了一个示例项目:https://github.com/ntnmrndn/WKUploadFormCrash 并向Apple填写了一份错误报告
推荐答案
如@jshapy8所述,您需要覆盖present()
方法并手动设置.sourceView
/.sourceFrame
/.barButtonItem
。
但您需要记住,如果保存WkWebView
的UIViewController
由UINavigationController
呈现,则UINavigationController
将负责呈现其他UIViewController
。
除非您使用的是iPad。
因此,实际上您需要重写UINavigationController
中的present()
方法以及保存WkWebView
的UIViewController
方法。
在下面的示例中,存放WkWebView
的UIViewController
称为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的另一个漏洞,因为这只是iPhone上的问题,而不是iPad上的问题(刚刚在搭载iOS 12和iOS;13的iPad上测试了这个问题)。
看起来苹果工程师只是忘记了,如果WKWebView
被呈现为他们新的模式呈现风格,UIDocumentMenuViewController
被呈现为UIModalPresentationPopover
风格,甚至在手机上也是如此,这在iOS 13之前根本不是这样的。
我更新了我的代码,现在它只为电话类型设置.sourceView
/.sourceFrame
/.barButtonItem
,因为iOS It本身会正确处理平板电脑类型。
这篇关于调用文件选取器时,带有新iOS13模式崩溃的WKWebview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!