Я хочу создать приложение веб-просмотра stati c на Swift. HTML и CSS загружаются, но не локальные файлы javascript. (это jquery, bootstrap, popper и мои скрипты)
Я пробовал это:
override func viewDidLoad() {
super.viewDidLoad()
let url = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "assets")!
loadJS()
webView.loadFileURL(url, allowingReadAccessTo: url)
}
func loadJS() {
let jquery = Bundle.main.url(forResource: "jquery", withExtension: "js", subdirectory: "assets/js")!
let bootstrap = Bundle.main.url(forResource: "bootstrap", withExtension: "js", subdirectory: "assets/js")!
let popper = Bundle.main.url(forResource: "popper", withExtension: "js", subdirectory: "assets/js")!
let modules = Bundle.main.url(forResource: "modules", withExtension: "js", subdirectory: "assets/js")!
do {
let content1 = try String(contentsOfFile: jquery.path, encoding: String.Encoding.utf8)
let content2 = try String(contentsOfFile: bootstrap.path, encoding: String.Encoding.utf8)
let content3 = try String(contentsOfFile: popper.path, encoding: String.Encoding.utf8)
let content4 = try String(contentsOfFile: modules.path, encoding: String.Encoding.utf8)
let controller = WKUserContentController()
controller.addUserScript(WKUserScript(source: content1, injectionTime: WKUserScriptInjectionTime.atDocumentStart, forMainFrameOnly: true))
controller.addUserScript(WKUserScript(source: content2, injectionTime: WKUserScriptInjectionTime.atDocumentStart, forMainFrameOnly: true))
controller.addUserScript(WKUserScript(source: content3, injectionTime: WKUserScriptInjectionTime.atDocumentStart, forMainFrameOnly: true))
controller.addUserScript(WKUserScript(source: content4, injectionTime: WKUserScriptInjectionTime.atDocumentStart, forMainFrameOnly: true))
print(controller.userScripts)
} catch {
print(error)
}
}