iOS WKWebView javascript警报崩溃在ipad上 [英] iOS WKWebView javascript alert crashing on ipad
问题描述
我正在使用此答案中的解决方案,该解决方案正确显示了javascript alert / confirm / etc框,并且效果很好在iphone上。
I'm using the solution in this answer that properly displays javascript alert/confirm/etc boxes and it works great on iphones.
func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo,
completionHandler: @escaping () -> Void) {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
completionHandler()
}))
present(alertController, animated: true, completion: nil)
}
func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo,
completionHandler: @escaping (Bool) -> Void) {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
completionHandler(true)
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
completionHandler(false)
}))
present(alertController, animated: true, completion: nil)
}
func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo,
completionHandler: @escaping (String?) -> Void) {
let alertController = UIAlertController(title: nil, message: prompt, preferredStyle: .actionSheet)
alertController.addTextField { (textField) in
textField.text = defaultText
}
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
if let text = alertController.textFields?.first?.text {
completionHandler(text)
} else {
completionHandler(defaultText)
}
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
completionHandler(nil)
}))
present(alertController, animated: true, completion: nil)
}
但是在ipads上点击任何内容使用javascript操作使应用程序崩溃 [UIPopoverPresentationController presentationTransitionWillBegin]
However on ipads clicking anything with a javascript action crashes the app with [UIPopoverPresentationController presentationTransitionWillBegin]
致命异常:NSGenericException
您的应用程序提供了样式UIAlertControllerStyleActionSheet的UIAlertController()。具有此样式的UIAlertController的modalPresentationStyle是UIModalPresentationPopover。您必须通过警报控制器的popoverPresentationController为此弹出窗口提供位置信息。您必须提供sourceView和sourceRect或barButtonItem。如果在显示警报控制器时不知道此信息,您可以在UIPopoverPresentationControllerDelegate方法-prepareForPopoverPresentation中提供。
Fatal Exception: NSGenericException Your application has presented a UIAlertController () of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.
我是有点不知所措来解决这个问题。任何人都可以就此提出一些建议吗?我已经尝试了各种方法来使用
I'm sort of at a loss here to fix this. Can anyone give some advice on this? I have tried various ways to use
UIPopoverPresentationControllerDelegate方法
-prepareForPopoverPresentation。'
the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'
在我的代码中,但没有成功。任何建议对此表示赞赏。谢谢。
in my code, but wasn't successful. Any advice appreciated on this. Thanks.
推荐答案
想出这个 - 希望给其他任何人留下一张便条。对函数结尾的一个小改动:
Figured this one out - wanted to leave a note for anyone else that runs in to this. a small change to the end of the functions:
替换
present(alertController, animated: true, completion: nil)
with
if let presenter = alertController.popoverPresentationController {
presenter.sourceView = self.view
}
self.present(alertController, animated: true, completion: nil)
这篇关于iOS WKWebView javascript警报崩溃在ipad上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!