在Webview iOS中插入JavaScript代码
我用swift语言代码创建了一个iOS网络浏览器。并添加一个额外的按钮在该网页上插入脚本,但当我尝试此操作时,它总是崩溃:在Webview iOS中插入JavaScript代码,javascript,iphone,xcode,swift,ios8,Javascript,Iphone,Xcode,Swift,Ios8,我用swift语言代码创建了一个iOS网络浏览器。并添加一个额外的按钮在该网页上插入脚本,但当我尝试此操作时,它总是崩溃: webView!.evaluateJavaScript("document.body.style.background = 'red';", nil) 你知道怎么解决这个问题吗?以及如何从文件中读取JavaScript代码,然后将其注入webview元素 我使用代码样式作为示例,但使用WKWebView时: 如果你能解决这个问题,我需要一个基本的工作代码。以及如何从文件
webView!.evaluateJavaScript("document.body.style.background = 'red';", nil)
你知道怎么解决这个问题吗?以及如何从文件中读取JavaScript代码,然后将其注入webview元素
我使用代码样式作为示例,但使用WKWebView时:
如果你能解决这个问题,我需要一个基本的工作代码。以及如何从文件加载JavaScript的解决方案。并将该代码注入WKWebView元素。我看不到您当前使用的方法(evaluateJavaScript),但它在中。也许您使用了错误的API?或许可以尝试使用: 另外,我不确定是否需要“!”(直觉告诉我不需要),因为代码中没有上下文。因此,也许可以尝试两种版本 从文件中获取字符串类似于:
let path = NSBundle.mainBundle().pathForResource("jsFileName", ofType: "js")
if let content = String.stringWithContentsOfFile(path, encoding: NSUTF8StringEncoding, error: nil) {
println("js content is \(content)")
}
从磁盘加载有很多关于文件复制和存储方式的变量,因此您必须做一些工作,以使
路径
变量适合您的结构。如果您使用WKWebView
,这里有一个解决方案
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)
}
}
}
这将适用于WKWebView。别忘了在类定义的顶部添加WebKit框架
var webView: WKWebView!
func loadWebViewWithCustomJavaScript {
//Create Preferences
let preferences = WKPreferences()
preferences.javaScriptEnabled = true
//Initialise javascript file with user content controller and configuration
let configuration = WKWebViewConfiguration()
let scriptURL = NSBundle.mainBundle().pathForResource("Your File Name", ofType: "js")
var scriptContent = ""
do {
scriptContent = try String(contentsOfFile: scriptURL!, encoding: NSUTF8StringEncoding)
} catch{
print("Cannot Load File")
}
let script = WKUserScript(source: scriptContent, injectionTime: .AtDocumentStart, forMainFrameOnly: true)
configuration.userContentController.addUserScript(script)
configuration.preferences = preferences
//Create WebView instance
webView = WKWebView(frame: CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height), configuration: configuration)
view.addSubview(webView)
//Finally load the url
let url = NSURL(string:"your URL")
let urlRequest = NSURLRequest(URL: url!)
webView.loadRequest(urlRequest)
}
用于注入的JavaScript代码示例
//Hides name of "background1" element on the page
var styleTag = document.createElement("style");
styleTag.textContent = 'div#background1, .after-post.widget-area {display:none;}';
document.documentElement.appendChild(styleTag);
使用以下命令使其正常工作。使用字符串而不是NSString来使用本机swift 2。要注入的Javascript代码必须位于hidesections.js文件中
let jsPath = NSBundle.mainBundle().pathForResource("hidesections", ofType: "js");
let jsContent: String?
do
{
jsContent = try String(contentsOfFile: jsPath!, encoding: NSUTF8StringEncoding)
}
catch _
{
jsContent = nil
}
webView.stringByEvaluatingJavaScriptFromString(jsContent!)
你能提供一些关于崩溃的信息吗?“线程1:信号SIGABRT”你使用的是
WKWebView
还是UIWebView
?我已经将github中的示例转换为WKWebView。我使用WKWebViewStill错误,你能给我提供一个基本的代码示例吗?我使用WKWebview,并给我这个错误:“WKWebview”没有名为“stringByEvaluatingJavaScriptFromString”的成员,如果你尝试你的原始代码,但像webView.evaluateJavaScript(“document.body.style.background='red';”,completionHandler:nil)
,像没有“!”吗?当我点击按钮(使用此代码)时,你能在该webView上调用任何方法而没有问题吗。一切都消失了,我没有看到web视图元素,只有一个带有长滚动条的白色页面。这里仍然看不到任何内容。
let jsPath = NSBundle.mainBundle().pathForResource("hidesections", ofType: "js");
let jsContent: String?
do
{
jsContent = try String(contentsOfFile: jsPath!, encoding: NSUTF8StringEncoding)
}
catch _
{
jsContent = nil
}
webView.stringByEvaluatingJavaScriptFromString(jsContent!)