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

Posted

技术标签:

【中文标题】从 Documents 目录在 UIWebview 中打开时,ios-css 和 js 无法在 html 文件中链接【英文标题】:ios-css and js not working linked in html file when opening in UIWebview from Documents directory 【发布时间】:2013-11-13 07:30:44 【问题描述】:

我在 iosDocuments 目录 中有以下 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 的完整路径时,它对我来说工作正常。

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

提前致谢。

【问题讨论】:

没有看到它,我相信您以编程方式加载 index.html 文件的代码没有 baseURL 参数。我发布了包含更多详细信息的完整答案。 【参考方案1】:

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

查看 HTML 中的 BASE 元素,它位于

元素中:

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>

要解决这个问题,您需要手动提供一个文档库 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

【讨论】:

感谢您的建议。这真的很有帮助。我更新了我的正确答案。【参考方案2】:

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

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

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

可能是你加载js的麻烦。

【讨论】:

实际上我从服务器上传 .zip 文件并将该 .zip 文件下载到带有 www 文件夹的文档中。现在我想从这个路径打开 index.html。 感谢您的回答。请在汤姆佩斯的回答中查看我的更新答案。这对我有用。【参考方案3】:

试试这个……

希望能解决你的问题

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

下图是添加 HTML 的,如果是在项目外添加文件,也可以点击复制。如果文件已经存在,则无需再次复制。

【讨论】:

我知道这种方法效果很好。但我想从 document\www 文件夹打开。,因为我的 www 文件夹没有修复,这就是原因 试试这个..在你的项目中手动创建一个文件夹'document',然后在其中创建'www'文件夹。然后将这些文件夹作为文件夹引用添加到您的项目中。然后试试上面的方法。

以上是关于从 Documents 目录在 UIWebview 中打开时,ios-css 和 js 无法在 html 文件中链接的主要内容,如果未能解决你的问题,请参考以下文章

UIWebView 从本地文件系统加载 HTML 文件

将视频从 Documents 目录存储到相册

从 Documents 目录存储和读取文件 iOS 5

使用 xcode 从 Documents 目录中读取文件

通过 iTunes 从 App 的 Documents 目录访问文件夹

如何将 SQLite 文件从 Documents 目录保存回目标 C 中的主包?