显然,以编程方式加载HTML文件时,文档库与应用程序的Documents目录不同.
查看HTML中的BASE元素,它位于< head>内.元件:
gt;
Meta charset="utf-8">
要解决此问题,您将手动提供文档基本URL.您还没有发布代码如何以编程方式加载index.html文件,因此我假设您正在使用以下UIWebView方法:
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
所以现在试试这段代码,看看它是否解决了你的问题:
// Get the app Documents path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
// Add www/index.html to the basepath
NSString *fullFilepath = [basePath stringByAppendingPathComponent:@"www/index.html"];// This must be filled in with your code
// Load the file,assuming it exists
NSError *err;
NSString *html = [NSString stringWithContentsOfFile:fullFilepath encoding:NSUTF8StringEncoding error:&err];
// Load the html string into the web view with the base url
[self.webview loadHTMLString:html baseURL:[NSURL URLWithString:fullFilepath]];
如果这不起作用,请尝试更新从.zip文件获取的HTML文件的代码.就像是:
// Get the app Documents path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
// Add www/index.html to the basepath
NSError *err;
NSString *fullFilepath = [basePath stringByAppendingPathComponent:@"www/index.html"];
// Load the file,assuming it exists
NSString *html = [NSString stringWithContentsOfFile:fullFilepath encoding:NSUTF8StringEncoding error:&err];
// Check to ensure base element doesn't already exist
if ([html rangeOfString:@"