HTML   发布时间:2022-04-14  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在DocumentsWeb目录中打开UIWebview时,ios-css和js在html文件中无法链接大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我在ios的Documents目录中有我的www文件夹的结构

Documents
   --www
      --index.html
      --js
          --script.js
      --css
          --style.css
@H_874_8@

我的index.html文件引用脚本和样式文件,如下所示:

@H_874_8@

请注意,文件和目录结构是在应用程序启动后从远程下载和解压缩的zip文件中创建的.因此,这不是由于Xcode文件夹组与应用程序包的Documents子文件夹混淆而导致的问题.

它在浏览器中运行时工作正常但在以编程方式在UIWebview中加载index.html时,脚本和样式不起作用.同时索引文件本身加载完美.

当我在index.html文件中提供脚本和css的完整路径时,它对我来说很好.

为什么相对路径不起作用?

提前致谢.

最佳答案
@H_618_30@显然,以编程方式加载HTML文件时,文档库与应用程序的Documents目录不同.

查看HTML中的BASE元素,它位于< head>内.元件:

http://www.w3.org/wiki/HTML/Elements/base

title>This is an example for the title>
        http://www.example.com/news/index.html">
    s.html">archives@H_874_8@

要解决此问题,您将手动提供文档基本URl.您还没有发布代码如何以编程方式加载index.html文件,因此我假设您正在使用以下UIWebView方法:

- (void)loadHTMLString:(NSString *)String baseURL:(NSURL *)baseURL
@H_874_8@

所以现在试试这段代码,看看它是否解决了你的问题:

// 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]];
@H_874_8@

如果这不起作用,请尝试更新从.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:@"LOCATIOn == NsnotFound) {
    // HTML doesn't contain a base url tag
    // replace head with head and base tag
    NSString *headreplacement = [NSString StringWithFormat:@"StringByreplacingoccurrencesOfString:@"String:headreplacement];
    [html writeToFile:fullFilepath atomically:YES encoding:NSUTF8StringEncoding error:&err];
}
// Now the file has a base url tag
@H_874_8@

大佬总结

以上是大佬教程为你收集整理的在DocumentsWeb目录中打开UIWebview时,ios-css和js在html文件中无法链接全部内容,希望文章能够帮你解决在DocumentsWeb目录中打开UIWebview时,ios-css和js在html文件中无法链接所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。