使用iOS8 Swift XCode 6通过uiwebview将新样式表注入网站 [英] Injecting a new stylesheet into a website via uiwebview using iOS8 Swift XCode 6
问题描述
我在这里只看到了一些使用Objective-C的选项,但是我在使用XCode 6中的Swift iOS8时遇到了麻烦。我正在使用uiwebview来加载一个网站。例如,让我们说它的google.com。
I've seen a few options on here using just Objective-C, but I'm having trouble doing this with Swift iOS8 in XCode 6. I'm using the uiwebview to load a website. For sake of example, let's say its google.com.
@IBOutlet var website: UIWebView!
var url = "http://www.google.com"
func loadUrl() {
let requestURL = NSURL(string: url)
let request = NSURLRequest(URL: requestURL!)
website.loadRequest(request)
}
override func viewDidLoad() {
super.viewDidLoad()
website.delegate = self
loadUrl()
}
func webViewDidFinishLoad(website: UIWebView) {
var jsscript = "some script here"
website.stringByEvaluatingJavaScriptFromString(jsscript)
}
使用Swift,我将如何注入一个全新的样式表进入网站,所以我可以覆盖许多样式?
Using Swift, how would I inject a whole new stylesheet into the website so I can overwrite many of the styles?
另外,我希望新的样式表存在于我的应用程序目录中。那个最好的目录在哪里?在支持文件?下而且,我如何在我的代码中调用该路径?
Also, I'd like the new stylesheet to exist in one of my app's directories. Where is the best directory for that to be? Under "Supporting files?" And, how would I call that path in my code?
如果在应用样式之前不加载网站也会很好。
It would also be nice if the website wouldn't load until the styles had been applied.
推荐答案
所以,我认为至少有一种方法可以使用Swift为正在加载的网页添加样式:
So, I think I found at least one way to accomplish appending styles to the webpage being loaded using Swift:
var loadStyles = "var script =
document.createElement('link');
script.type = 'text/css';
script.rel = 'stylesheet';
script.href = 'http://fake-url/styles.css';
document.getElementsByTagName('body')[0].appendChild(script);"
website.stringByEvaluatingJavaScriptFromString(loadStyles)
这篇关于使用iOS8 Swift XCode 6通过uiwebview将新样式表注入网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!