javascript - type - 如何在UIWebView中加載CSS文件?



xcode webview https (0)

我有一個CSS文件,我想加載在我的UIWebView 。 我在webViewDidFinishLoad方法中編寫了下面的代碼。 但它不工作。

我已經使用stringByEvaluatingJavaScriptFromString加載CSS文件。 我想將內容寬度設置為100%。

下面的腳本工作正常在android代碼(android studio)

- (void)webViewDidFinishLoad:(UIWebView *)webView{

NSString *path = [[NSBundle mainBundle] pathForResource:@"myCSS" ofType:@"css"];
NSString *cssString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];


dispatch_async(dispatch_get_global_queue(0, 0), ^{
    //load your data here.
    dispatch_async(dispatch_get_main_queue(), ^{

        [_webWhatsapp stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"javascript:(function() {var parent = document.getElementsByTagName('head')[0];var style = document.createElement('style');style.type = 'text/css';style.innerHTML = window.atob('%@');parent.appendChild(style)})();",cssString]];


});
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
    dispatch_async(dispatch_get_main_queue(), ^{

            [_webWhatsapp stringByEvaluatingJavaScriptFromString:@"javascript:document.getElementById('pane-side')[0].addEventListener('click',function f(e) {setTimeout(function(){ document.getElementById('main').style.width='100%%';document.getElementById('side').style.display='none';}, 1000);})()"];

    });
});    
}

下面的腳本在android studio的android代碼中工作正常

"@"javascript:(function() {var parent = document.getElementsByTagName('head')[0];var style = document.createElement('style');style.type = 'text/css';style.innerHTML = window.atob('%@');parent.appendChild(style)})();""

"@"javascript:document.getElementById('pane-side')[0].addEventListener('click',function f(e) {setTimeout(function(){ document.getElementById('main').style.width='100%%';document.getElementById('side').style.display='none';}, 1000);})()""




uiwebview