【问题标题】:what is the wrong when loading local resource files in webview in iphone在iphone的webview中加载本地资源文件时出现什么问题
【发布时间】:2011-11-29 07:07:34
【问题描述】:

我正在尝试将本地 css 和 html 文件加载到 WebView 中,但是 web 视图在 webview 中显示 css 文件的内容,如下图所示..

objective-c 代码:

NSString *htmlPath= [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"  inDirectory:NO];
//[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]];

// load css styles
NSString *cssPath   = [[NSBundle mainBundle] pathForResource:@"css" ofType:@"css"];
NSData *cssData     = [NSData dataWithContentsOfFile:cssPath];
NSString *cssString = [[NSString alloc] initWithData:cssData encoding:NSASCIIStringEncoding];

// load js
NSString *jsPath   = [[NSBundle mainBundle] pathForResource:@"MyJS" ofType:@"js"];
NSData *jsData     = [NSData dataWithContentsOfFile:jsPath];
NSString *jsString = [[NSString alloc] initWithData:jsData encoding:NSASCIIStringEncoding];                         

// compose full html page
NSString *pageContent = [NSString stringWithFormat:@"%@%@%@", cssString, jsString, htmlPath];
[webView loadHTMLString:pageContent baseURL:[NSURL URLWithString:@""]];

有什么问题?

【问题讨论】:

    标签: iphone html objective-c css uiwebview


    【解决方案1】:

    你不能使用

              [webView loadHTMLString:pageContent baseURL:[NSURL URLWithString:@""]];
    

    而 loadHTMLString 尝试使用 stringByEvaluatingJavaScriptFromString

    如果您有任何问题,请查看以下帖子。我想你会很容易得到它的

    UIWebView and local css file

    我还没有将它用于 CSS,但希望这会对你有所帮助...祝你好运...

    【讨论】:

    • 你能举例说明如何使用 stringByEvaluatingJavaScriptFromString 吗?
    • NSString *js = @"document.getElementsByTagName('link')[0].setAttribute('href','"; NSString *js2 = [js stringByAppendingString:cssPath]; NSString *finalJS = [js2 stringByAppendingString:@"');"]; [webview stringByEvaluatingJavaScriptFromString:finalJS];实际上这段代码来自我给出的链接......请检查它非常有用的链接......
    • 但是当 HTML 被加载?!
    猜你喜欢
    • 2012-02-02
    • 1970-01-01
    • 2019-04-17
    • 2011-07-26
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 2020-05-23
    相关资源
    最近更新 更多