WKWebView不显示提示框(Swift)
生活随笔
收集整理的這篇文章主要介紹了
WKWebView不显示提示框(Swift)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用WKWebView的時候會出現明明自己做的一些頁面有提示框, 為什么使用別人的頁面提示框總是不顯示, 其實很大部分原因是因為該提示框是通過JS調用的, 需要實現WKUIDelegate來進行監聽
// MARK: - WKUIDelegate// 監聽通過JS調用警告框func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) incompletionHandler()}))self.present(alert, animated: true, completion: nil)}// 監聽通過JS調用提示框func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) incompletionHandler(true)}))alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) incompletionHandler(false)}))self.present(alert, animated: true, completion: nil)}// 監聽JS調用輸入框func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) {// 類似上面兩個方法}這里需要注意, completionHandler一定要調用, 否則會出錯!
總結
以上是生活随笔為你收集整理的WKWebView不显示提示框(Swift)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS面试用到的知识点和技术点--第二章
- 下一篇: Java_Shell多线程