我在ios的Documents目录中有我的www文件夹的结构
Documents
--www
--index.html
--js
--script.js
--css
--style.css
我的index.html文件引用脚本和样式文件,如下所示:
请注意,文件和目录结构是在应用程序启动后从远程下载和解压缩的zip文件中创建的.因此,这不是由于Xcode文件夹组与应用程序包的Documents子文件夹混淆而导致的问题.
它在浏览器中运行时工作正常但在以编程方式在UIWebview中加载index.html时,脚本和样式不起作用.同时索引文件本身加载完美.
当我在index.html文件中提供脚本和css的完整路径时,它对我来说很好.
为什么相对路径不起作用?
提前致谢.
最佳答案
显然,以编程方式加载HTML文件时,文档库与应用程序的Documents目录不同.
查看HTML中的BASE元素,它位于< head>内.元件:
http://www.w3.org/wiki/HTML/Elements/base
要解决此问题,您将手动提供文档基本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:@"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。