导入.js文件以快速

时间:2018-07-31 03:09:52

标签: ios swift xcode

我有一个jquery.min.js文件,其中存储了我网站的所有功能。我正在调用API以下载HTML和CSS。现在如何连接它们?我应该在调用API期间还是调用之后连接它们,然后在viewDidLoad中连接它们。

这就是我调用我的API的方式

func loadWebView(webView: UIWebView) {
        var loadWebViewURLComponent = urlComponents
        loadWebViewURLComponent.path = "/api/FAQ/"
        let loadWebViewURL = loadWebViewURLComponent.url

        var request = URLRequest(url: loadWebViewURL!)
        request.httpMethod = "GET"

        let token = KeychainService.loadPassword(service: "myService", account: defaults.string(forKey: "email")!)

        request.addValue("Token " + token!, forHTTPHeaderField: "Authorization")
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")

        webView.loadRequest(request)
}

这是我初始化jquery.min.js文件的方式

func initializeJS() {
        self.jsContext = JSContext()

        if let jsSourcePath = Bundle.main.path(forResource: "jquery.min", ofType: "js") {
            do {
                // Load its contents to a String variable.
                let jsSourceContents = try String(contentsOfFile: jsSourcePath)

                // Add the Javascript code that currently exists in the jsSourceContents to the Javascript Runtime through the jsContext object.
                let file = self.jsContext?.evaluateScript(jsSourceContents)

            }
            catch {
                print(error.localizedDescription)
            }
       }
 }

0 个答案:

没有答案
相关问题