美文网首页
iOS Swift-WKWebView

iOS Swift-WKWebView

作者: Lee坚武 | 来源:发表于2022-01-26 16:37 被阅读0次

    更多方法交流可以家魏鑫:lixiaowu1129,一起探讨iOS相关技术!

    涵盖的知识点:

    获取webView 加载url 链接中的参数
    WKWebView 在Swift 中的使用
    WKWebView 的navigationDelegate方法含义
    去掉WKWebView 键盘自带的工具栏

    1. 获取webView 加载url 链接中的参数
           // 通过URL 的query 参数获得所有的参数的一个字符串
            let url = URL(string: "http://www.example.com/response?code=1234")
            guard let u = url else {
                return
            }
            
            let query = u.query
            guard let q = query else {
                return
            }
            print(q) // code=1234
            // 以字符串截取的形式 来获取想要的参数
            let code = String(q["code=".endIndex...])
            print(code) // 1234
    
    1. WKWebView 在Swift 中的使用

    Swift 5.0之后 使用WKWebView 需要引入头文件import WebKit, 且UIWebView 在4月份就被彻底废弃了.

    1. WKWebView 的navigationDelegate方法含义
        // 页面开始加载时调用
        func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
            
        }
        // 当内容开始返回时调用
        func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
            
        }
        // 页面加载完成之后调用
        func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
            
        }
        // 页面加载失败时调用
        func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
            
        }
        // 接收到服务器跳转请求之后调用
        func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
            
        }
        // 在收到响应后,决定是否跳转 -> 默认允许
        func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
            //允许跳转
            decisionHandler(.allow)
            //不允许跳转
    //        decisionHandler(.cancel)
        }
        // 在发送请求之前,决定是否跳转 -> 默认允许
        func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, preferences: WKWebpagePreferences, decisionHandler: @escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void) {
            decisionHandler(.allow, preferences)
        }
    
    1. 去掉WKWebView 键盘自带的工具栏
    需要去掉工具栏的WKWebView 直接调用hack_removeInputAccessory即可.
    
    // MARK: 去掉WKWebView自带的 工具栏
    fileprivate final class InputAccessoryHackHelper: NSObject {
        @objc var inputAccessoryView: AnyObject? { return nil }
    }
    
    extension WKWebView {
        func hack_removeInputAccessory() {
            guard let target = scrollView.subviews.first(where: {
                String(describing: type(of: $0)).hasPrefix("WKContent")
            }), let superclass = target.superclass else {
                return
            }
            
            let noInputAccessoryViewClassName = "\(superclass)_NoInputAccessoryView"
            var newClass: AnyClass? = NSClassFromString(noInputAccessoryViewClassName)
            
            if newClass == nil, let targetClass = object_getClass(target), let classNameCString = noInputAccessoryViewClassName.cString(using: .ascii) {
                newClass = objc_allocateClassPair(targetClass, classNameCString, 0)
                
                if let newClass = newClass {
                    objc_registerClassPair(newClass)
                }
            }
            
            guard let noInputAccessoryClass = newClass, let originalMethod = class_getInstanceMethod(InputAccessoryHackHelper.self, #selector(getter: InputAccessoryHackHelper.inputAccessoryView)) else {
                return
            }
            class_addMethod(noInputAccessoryClass.self, #selector(getter: InputAccessoryHackHelper.inputAccessoryView), method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
            object_setClass(target, noInputAccessoryClass)
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS Swift-WKWebView

          本文链接:https://www.haomeiwen.com/subject/oamnhrtx.html