【发布时间】:2017-03-13 20:37:48
【问题描述】:
我正在使用this answer 中的解决方案,该解决方案可以正确显示 javascript 警报/确认/等框,并且在 iphone 上运行良好。
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)
}
但是,在 ipad 上点击任何带有 javascript 操作的内容会使应用程序崩溃,并显示 [UIPopoverPresentationController presentationTransitionWillBegin]
致命异常:NSGenericException 您的应用程序呈现了一个 UIAlertController() 样式的 UIAlertControllerStyleActionSheet。具有此样式的 UIAlertController 的 modalPresentationStyle 是 UIModalPresentationPopover。您必须通过警报控制器的 popoverPresentationController 提供此弹出窗口的位置信息。您必须提供 sourceView 和 sourceRect 或 barButtonItem。如果在呈现警报控制器时不知道此信息,则可以在 UIPopoverPresentationControllerDelegate 方法 -prepareForPopoverPresentation 中提供。
我有点不知所措,无法解决这个问题。任何人都可以对此提出一些建议吗?我尝试了各种使用方式
UIPopoverPresentationControllerDelegate 方法 -prepareForPopoverPresentation。'
在我的代码中,但没有成功。对此表示赞赏的任何建议。谢谢。
【问题讨论】:
标签: javascript ios ipad wkwebview