0
我试图在xCode上加载一个页面,然后用另一个文件覆盖它的样式。我已经在Chrome中手动测试了这一点,页面的两个部分应该变成鲜红色,但它们不在我的XCode项目中。有任何想法吗?JavaScript将CSS注入Swift WebView
class ViewController: UIViewController {
var urlPath = "http://grappul.com"
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
loadAddress()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadAddress(){
let requestURL = NSURL(string:urlPath)
let request = NSURLRequest(URL: requestURL!)
webView.loadRequest(request)
println("got the page!")
webViewDidFinishLoad(webView)
}
func webViewDidFinishLoad(website: UIWebView){
var jsscript = "var script = document.createElement('link');
script.type = 'text/css';
script.rel = 'stylesheet';
script.href = 'http://www.grappul.com/snapkin/restyle.css';
document.getElementByTagName('head')[0].appendChild(script);"
println("got the css!")
website.stringByEvaluatingJavaScriptFromString(jsscript)
}
谢谢吨家伙!