Swift5.0 WKWebView中的JS交互

好的团队离不开大家的默契合作,在开发中经常遇到H5和移动端合作的业务,在开发中JS交互是非常常见的,小萌现在的主打语言是Swift,所以封装一下Swift版的JS交互

完整代码

在此之前加入协议WKNavigationDelegate,WKUIDelegate ,WKScriptMessageHandler

    lazy var userWebview:WKWebView = {
        //配置环境
        //初始化WKWebViewConfiguration
        let webConfiguration = WKWebViewConfiguration()
        let userContentController = WKUserContentController()
        webConfiguration.userContentController = userContentController


   //JS调用Swift
   // userContentController.add(self, name: "")


        let userWebview = WKWebView(frame: CGRect.zero, configuration: webConfiguration)
        userWebview.frame = CGRect(x:SCREEN_WIDTH - 115, y:53, width:70, height:30)
        userWebview.uiDelegate = self
        userWebview.navigationDelegate = self
        userWebview.scrollView.isScrollEnabled = false
        userWebview.scrollView.showsVerticalScrollIndicator = false
        userWebview.scrollView.showsHorizontalScrollIndicator = false
        let myURL = URL(string: PublicGetVerify)
        let myRequest = URLRequest(url: myURL!)
        userWebview.load(myRequest)

        return userWebview
    }()
    
//MARK: - WKScriptMessageHandler
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        
    }
    
    
    //MARK: - WKNavigationDelegate
    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, didFail navigation: WKNavigation!, withError error: Error) {
        // 页面加载完成之后调用
    }
    
    
    func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
         接收到服务器跳转请求之后调用
    }

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        
        let url = navigationAction.request.url
        
        let scheme = url?.scheme
        
        guard let schemeStr = scheme else { return
            
        }
        if schemeStr == "tel" {
            //iOS10 改为此函数
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(url!, options: [String : Any](), completionHandler: nil)
            } else {
                // Fallback on earlier versions
            }
        }
        
        decisionHandler(.allow)
     // 在发送请求之前,决定是否跳转
    }

    func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
        
        
        decisionHandler(.allow)
        
        // 在收到响应后,决定是否跳转
    }
    
   
    //MARK: - WKUIDelegate
    // 创建一个新的WebView
    func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
        
        return WKWebView()
    }
    
    // 输入框
    // JS端调用prompt函数时,会触发此方法
    // 要求输入一段文本
    // 在原生输入得到文本内容后,通过completionHandler回调给JS
    func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void){
        
    
        
    }
    
    
    // 确认框
    // JS端调用confirm函数时,会触发此方法
    // 通过message可以拿到JS端所传的数据
    // 在iOS端显示原生alert得到YES/NO后
    // 通过completionHandler回调给JS端
    func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void){

        
    }
    
    
    // 警告框
    // 在JS端调用alert函数时,会触发此代理方法。
    // JS端调用alert时所传的数据可以通过message拿到
    // 在原生得到结果后,需要回调JS,是通过completionHandler回调
    func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void){
        
  
    }

你可能感兴趣的:(swift)