5

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

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

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

<link rel="stylesheet" href="css/style.css" />
<script type="text/javascript" src="js/script.js"></script>

请注意,文件和目录结构是在应用程序启动后从远程下载和提取的 zip 文件创建的。所以这不是由 Xcode 文件夹组与应用程序包的 Documents 子文件夹混淆引起的问题。

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

当我在 index.html 文件中给出脚本和 css 的完整路径时,它对我来说工作正常。

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

提前致谢。

4

3 回答 3

4

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

查看 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>

要解决这个问题,您需要手动提供一个 Document 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, 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
于 2013-12-13T07:32:02.887 回答
3

可能是你的 JS 和 CSS 文件不是由 xcode 编译的。使用 xcode 打开您的项目并转到“Targets”并检查“Compile Sources”部分。如果您的 js 文件存在于该文件夹中,请将其移动到“复制捆绑资源”并从“编译源”文件夹中删除。

之后从您的设备中删除应用程序,然后转到 xcode 中的构建菜单并清理所有目标并重新编译。

检查您的 HTML 文件是否包含您的 js 文件。

加载js可能是你的麻烦。

于 2013-12-13T06:54:07.267 回答
3

试试这个 ...

我希望这能解决你的问题

我也遇到了同样的问题,我在将文件添加到 XCode 时通过做简单的事情解决了它我做了一个小改动,如下图所示:您必须通过选择为任何文件夹选项创建文件夹引用来添加文件

下图中的一个是添加 HTML,如果您是从项目外部添加文件,也请单击复制。如果文件已经存在,则无需再次复制。

在此处输入图像描述

于 2013-12-13T07:13:53.337 回答