I created a iOS web browser with swift language code. And add an extra button to inject a script on that web page, but it always crash when I try this:
webVi
If you are using WKWebView here is a solution.
if let scriptFile = NSBundle.mainBundle().pathForResource("script", ofType: "js") {
var error: NSError?
let scriptString = NSString(contentsOfFile: scriptFile, encoding: NSUTF8StringEncoding, error: &error)
if let error = error {
println("Error: Could not load script file => \(error)")
} else {
if let scriptString = scriptString {
let script = WKUserScript(source: scriptString, injectionTime: .AtDocumentEnd, forMainFrameOnly: true)
let controller = WKUserContentController()
controller.addUserScript(script)
let configuration = WKWebViewConfiguration()
configuration.userContentController = controller
let webView = WKWebView(frame: self.view.bounds, configuration: configuration)
self.webView = webView
self.view.addSubview(webView)
}
}
}