ipad 上未显示 wkewbview 警报对话框

wkewbview alert dialog not showing on ipad

我用的是 WKWebView。

警告对话框在 iPhone 上正常工作,但在 iPad 上不可见。我该如何解决这个问题?

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()
    }))
    
    
    if UIDevice.current.userInterfaceIdiom == .pad {

        if let popoverPresentationController = alertController.popoverPresentationController {
            
            popoverPresentationController.sourceView = self.view
            popoverPresentationController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
            
            popoverPresentationController.permittedArrowDirections = []

        }

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

}

在 iPad 的情况下,您没有在 if - let 中显示 ViewController。 将 self.present(alertController, animated: true, completion: nil) 移到 else 块之外。

if UIDevice.current.userInterfaceIdiom == .pad {

        if let popoverPresentationController = alertController.popoverPresentationController {
            
            popoverPresentationController.sourceView = self.view
            popoverPresentationController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
            
            popoverPresentationController.permittedArrowDirections = []

        }

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

作为 side-note,检查 alertController.popoverPresentationController 是否不为 nil