Intercept request with WKWebView

前端 未结 6 591
遇见更好的自我
遇见更好的自我 2021-02-04 05:32

Now i\'m using UIWebView and with canInitWithRequest: of NSURLProtocol i can intercept all requests and do with it what I want.

In

6条回答
  •  清酒与你
    2021-02-04 06:17

    I know I am late but I am able to solve this problem. I can intercept each and every request even your http/https call using below trick. I can also trace the call made from html to server calls. I can also use this to render html with offline content.

    1. Download the html of the website that we want to render in offline or online to intercept the request.
    2. Either place the html in document directory of the user or place it inside the archive. But we should know the path of the html file.
    3. Place all your js, cs, woff, font of our website at the same level as our base html. We need to given permission while loading the web view.
    4. Then we have to register our own custom handler scheme with WKWebView. When wkwebview see the pattern "myhandler-webview" then it will give you control and you will get the callback to 'func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask)' delegate implementation. You can play around with url in this delegate like mentioned in point 6.
          let configuration = WKWebViewConfiguration();
            configuration.setURLSchemeHandler(self, forURLScheme: "myhandler-webview");
            webView = WKWebView(frame: view.bounds, configuration: configuration);
    
    1. Convert file scheme to the custom scheme (myhandler-webview) then load it with WKWebView
        let htmlPath = Bundle.main.path(forResource: "index", ofType: "html")
        var htmlURL = URL(fileURLWithPath: htmlPath!, isDirectory: false)                    
        htmlURL = self.changeURLScheme(newScheme: "myhandler-webview", forURL: htmlURL)
        self.webView.load(URLRequest(url: htmlURL))
    
    1. Implement below methods of WKURLSchemeHandler protocol and handle didReceiveResponse, didReceiveData, didFinish delegate methods of WKURLSchemeTask.
        func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask) {
            print("Function: \(#function), line: \(#line)")
            print("==> \(urlSchemeTask.request.url?.absoluteString ?? "")\n")
    
        // You can find the url pattern by using urlSchemeTask.request.url. and create NSData from your local resource and send the data using 3 delegate method like done below.
        // You can also call server api from this native code and return the data to the task.
        // You can also cache the data coming from server and use it during offline access of this html.
        // When you are returning html the the mime type should be 'text/html'. When you are trying to return Json data then we should change the mime type to 'application/json'.
        // For returning json data you need to return NSHTTPURLResponse which has base classs of NSURLResponse with status code 200. 
    
        // Handle WKURLSchemeTask delegate methods
            let url = changeURLScheme(newScheme: "file", forURL: urlSchemeTask.request.url!)
    
            do {
                let data = try Data(contentsOf: url)
    
                urlSchemeTask.didReceive(URLResponse(url: urlSchemeTask.request.url!, mimeType: "text/html", expectedContentLength: data.count, textEncodingName: nil))
                urlSchemeTask.didReceive(data)
                urlSchemeTask.didFinish()
            } catch {
                print("Unexpected error when get data from URL: \(url)")
            }
        }
    
        func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) {
            print("Function: \(#function), line: \(#line)")
            print("==> \(urlSchemeTask.request.url?.absoluteString ?? "")\n")
        }
    
    

    Let me know if this explanation is not enough.

    Objective c example mentioned below

    intercepting request with wkwebview

提交回复
热议问题