iOS WKWebView javascript 警报在 ipad 上崩溃

iOS WKWebView javascript alert crashing on ipad

我正在使用 this answer 中的解决方案,它可以正确显示 javascript alert/confirm/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]

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.

我有点不知所措,无法解决这个问题。任何人都可以对此提出一些建议吗?我尝试过各种使用方式

the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'

在我的代码中,但没有成功。任何建议对此表示赞赏。谢谢。

解决了这个问题 - 想为遇到此问题的任何其他人留下便条。函数末尾的小改动:

替换

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

    if let presenter = alertController.popoverPresentationController {
        presenter.sourceView = self.view
    }

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