2015-04-05 52 views
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) 
} 

謝謝噸傢伙!

回答