iOS WKWebView javascript警报在ipad上崩溃

iOS WKWebView javascript警报在ipad上崩溃,javascript,ios,ipad,wkwebview,Javascript,Ios,Ipad,Wkwebview,我使用的解决方案能够正确显示javascript警报/确认/etc框,并且在iPhone上运行良好 func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) { let alertCon

我使用的解决方案能够正确显示javascript警报/确认/etc框,并且在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]

致命异常:NSGenericeException 您的应用程序提供了UIAlertControllerStyleActionSheet样式的UIAlertController()。具有此样式的UIAlertController的modalPresentationStyle为UIModalPresentationPopover。您必须通过警报控制器的popoverPresentationController提供此popover的位置信息。必须提供sourceView和sourceRect或barButtonItem。如果在演示警报控制器时不知道此信息,可以在UIPopoverPresentationControllerDelegate方法-PrepareForPoverPresentation中提供

我有点不知所措,无法解决这个问题。有人能给点建议吗?我试过各种方法来使用它

UIPopoverPresentationControllerDelegate方法 -准备正式演示文稿。”


在我的代码中,但没有成功。任何关于这方面的建议都将不胜感激。谢谢。

解决了这个问题-想给遇到这个问题的其他人留个便条。对功能末尾的小更改:

替换

    present(alertController, animated: true, completion: nil)


如果您试图同时显示多个警报,是否遇到应用程序崩溃的问题?在iPhone上工作很好,但在iPad上崩溃。这对我也有用,谢谢!但是我们如何改变警报的显示方式。。。因为在我的例子中,在ipad的左角显示为一个小的警告框。
    if let presenter = alertController.popoverPresentationController {
        presenter.sourceView = self.view
    }

    self.present(alertController, animated: true, completion: nil)