使用 iOS8 Swift XCode 6 通过 uiwebview 将新样式表注入网站 [英] Injecting a new stylesheet into a website via uiwebview using iOS8 Swift XCode 6

查看:15
本文介绍了使用 iOS8 Swift XCode 6 通过 uiwebview 将新样式表注入网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看到了一些仅使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆