OGeek|极客世界-中国程序员成长平台

标题: ios - 在 Web View 中加载拦截 javascript 文件 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:59
标题: ios - 在 Web View 中加载拦截 javascript 文件

我正在使用 WKWebView 制作网页 View 应用

let url = NSURL(string: "http://www.example.com")
let request = NSURLRequest(url: url! as URL)
homeWebView.load(request as URLRequest)

But I want to intercept some javaScript file of this website before it loads to change functionality and make the app a bit native looking

任何帮助将不胜感激。

谢谢。



Best Answer-推荐答案


以下是完整的示例。希望对你有帮助。

import UIKit

class ViewController: UIViewController,UIWebViewDelegate {

    private var webView: UIWebView!

    override func loadView() {
        webView = UIWebView(frame: .zero)
        webView.delegate = self
        view = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // set the view controller title, navigation etc
        self.loadWebPage()
    }

    func loadWebPage() {
        let url = URL(string: "https://www.google.com.pk/")
        //    let localFileURL = URL(fileURLWithPath: Bundle.main.path(forResource: "ExampleHtmlFileName", ofType: "html", inDirectory: "HtmlFilesFolderName")!)
        let myRequest = URLRequest(url: url!)
        webView.loadRequest(myRequest)
    }

    //MARK: UIWebView Delegate
    func webViewDidFinishLoad(_ webView: UIWebView) {

        let jsScript = "exampleJSMethod()"
        _ = webView.stringByEvaluatingJavaScript(from: jsScript)
    }


}

关于ios - 在 Web View 中加载拦截 javascript 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49686478/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4