WKWebView 的一些小总结
使用在初始化上, // WKWebView let wkWeb = WKWebView(frame: view.bounds) // 一些代理 wkWeb.navigationDelegate = self wkWeb.uiDelegate = self // UIWebView let web = UIWebView(frame: view.bounds) // 一些代理 web.delegate = self 二者在初始化上还是蛮像的。一个图样的我。(逃 仔细翻开了
也就是可以用 后面再继续港 那几个协议WKNavigationDelegate
// 1)接受网页信息,决定是否加载还是取消。必须执行肥调 decisionHandler 。逃逸闭包的属性 func webView(_ webView: WKWebView,decidePolicyFor navigationAction: WKNavigationAction,decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { print("(#function)") } // 2) 开始加载 func webView(_ webView: WKWebView,didStartProvisionalNavigation navigation: WKNavigation!) { print("(#function)") } // 3) 接受到网页 response 后,可以根据 statusCode 决定是否 继续加载。allow or cancel,必须执行肥调 decisionHandler 。逃逸闭包的属性 func webView(_ webView: WKWebView,decidePolicyFor navigationResponse: WKNavigationResponse,decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) { print("(#function)") guard let httpResponse = navigationResponse.response as? HTTPURLResponse else { decisionHandler(.allow) return } let policy : WKNavigationResponsePolicy = httpResponse.statusCode == 200 ? .allow : .cancel decisionHandler(policy) } // 4) 网页加载成功 func webView(_ webView: WKWebView,didFinish navigation: WKNavigation!) { print("(#function)") } // 4) 加载失败 func webView(_ webView: WKWebView,didFail navigation: WKNavigation!,withError error: Error) { print("(#function)") print(error.localizedDescription) } WKUIDelegate主要讲讲网页
在 // MARK: alert func webView(_ webView: WKWebView,runJavaScriptAlertPanelWithMessage message: String,initiatedByFrame frame: WKFrameInfo,completionHandler: @escaping () -> Void) { let alert = UIAlertController(title: "这是本地代码弹窗",message: message,preferredStyle: .alert) lert.addAction(UIAlertAction(title: "ok",style: .cancel,handler: { _ in // 必须加入这个 肥调,不然会闪 (逃 completionHandler() })) present(alert,animated: true,completion: nil) } // MARK: comfirm func webView(_ webView: WKWebView,runJavaScriptConfirmPanelWithMessage message: String,completionHandler: @escaping (Bool) -> Void) { let alert = UIAlertController(title: "这是本地代码弹窗",preferredStyle: .alert) alert.addAction(UIAlertAction(title: "??",style: .default,handler: { _ in completionHandler(true) })) alert.addAction(UIAlertAction(title: "不??",handler: { _ in completionHandler(false) })) present(alert,completion: nil) } // MARK: prompt func webView(_ webView: WKWebView,runJavaScriptTextInputPanelWithPrompt prompt: String,defaultText: String?,completionHandler: @escaping (String?) -> Void) { let alert = UIAlertController(title: "这是本地代码弹窗",message: prompt,preferredStyle: .alert) alert.addTextField { textField in textField.placeholder = defaultText } alert.addAction(UIAlertAction(title: "ok",handler: { _ in completionHandler(alert.textFields?.last?.text) })) present(alert,completion: nil) } 踩坑网页适配有些网页在客户端上显示,会出现一些不适配的情况。使用 // 这句相当于给网页注入一个 <meta> 标签,<meta name="viewport" content="width=device-width"> let jsToScaleFit = "var meta = document.createElement('meta'); meta.setAttribute('name','viewport'); meta.setAttribute('content','width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);" let scaleToFitScript = WKUserScript(source: jsToScaleFit,injectionTime: .atDocumentEnd,forMainFrameOnly: true) let userController = WKUserContentController() userController.addUserScript(scaleToFitScript) let config = WKWebViewConfiguration() config.userContentController = userController let wkWeb = WKWebView(frame: view.bounds,configuration: config!) 不过,还是不太推荐客户端去注入适配代码。最好还是告知前端,让他们去搞定这问题。个人觉得,两端少点干涉还是比较好滴~~~ (逃 客户端 -> 网页有时间,我们需要客户端去调用前端的一些代码。e.g. // 比如获取网页内容高度 let jsToGetWebHeight = "document.body.offsetHeight" wkWeb?.evaluateJavaScript(jsToGetWebHeight,completionHandler: { (data,error) in print(error?.localizedDescription ?? "执行正确") // data 是一个 any 类型,因此需要做好类型判断 if let webHeight : CGFloat = data as? CGFloat { print(webHeight) } }) 网页 -> 客户端不像 首先,在初始化阶段,需要使用到
window.webkit.messageHandlers.<name>.postMessage(<messageBody>) 来发送消息。 上个栗子 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |