在DocumentsWeb目录中打开UIWebview时,ios-css和js在html文件中无法链接

Hir*_*iya 5 html relative-path uiwebview ipad ios

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

Documents
   --www
      --index.html
      --js
          --script.js
      --css
          --style.css
Run Code Online (Sandbox Code Playgroud)

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

<link rel="stylesheet" href="css/style.css" />
<script type="text/javascript" src="js/script.js"></script>
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

提前致谢.

Tom*_*ace 4

显然,以编程方式加载 HTML 文件时,文档库与应用程序的文档目录不同。

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

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

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>This is an example for the <base> element</title>
        <base href="http://www.example.com/news/index.html">
    </head>
    <body>
        <p>Visit the <a href="archives.html">archives</a>.</p>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

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

- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
Run Code Online (Sandbox Code Playgroud)

现在尝试一下这段代码,看看它是否能解决您的问题:

// 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]];
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,请尝试更新从 .zip 文件获取的 HTML 文件的代码。就像是:

// 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
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:@"<base"].location == NSNotFound) {
    // HTML doesn't contain a base url tag
    // Replace head with head and base tag
    NSString *headreplacement = [NSString stringWithFormat:@"<head><base href=\"%@\">", fullFilepath];
    html = [html stringByReplacingOccurrencesOfString:@"<head>" withString:headreplacement];
    [html writeToFile:fullFilepath atomically:YES encoding:NSUTF8StringEncoding error:&err];
}
// Now the file has a base url tag
Run Code Online (Sandbox Code Playgroud)