当从文档目录打开UIWebview时,ios-css和js没有链接到html文件中。

时间:2022-09-06 13:25:38

I have following structure of my www folder in Documents directory in ios

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

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

My index.html file references script and style files as below:

我的指数。html文件引用脚本和样式文件如下:

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

Please note, the files and directory structure are created after app launch, from a remote-downloaded and extracted zip file. So this is not an issue caused by Xcode folder groups being confused with the app bundle's Documents subfolders.

请注意,这些文件和目录结构是在app启动后从一个远程下载和提取的zip文件中创建的。所以这不是Xcode文件夹组与app bundle的文档子文件夹混淆所导致的问题。

It works fine when run in a browser but when loading index.html in UIWebview programmatically, script and style are not working. Meanwhile the index file itself loads perfectly.

它在浏览器中运行时可以正常工作,但在加载索引时可以正常工作。以编程方式在UIWebview中使用的html,脚本和样式都不起作用。与此同时,索引文件本身也加载得很好。

When I give full path of script and css in index.html file it working fine for me.

当我在索引中给出脚本和css的完整路径时。html文件对我来说很好。

Why relative path not working?

为什么相对路径不工作?

Thanks in advance.

提前谢谢。

3 个解决方案

#1


4  

Apparently the document base is not the same as the app's Documents directory when loading the HTML file programatically.

显然,在程序化加载HTML文件时,文档库与应用程序的文档目录不同。

Check out the BASE element in HTML, it goes within the <head> element:

查看HTML中的基本元素,它进入元素:

http://www.w3.org/wiki/HTML/Elements/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>

To fix that, you'll have supply a Document base url manually. You haven't posted the code how you're programmatically loading the index.html file, so I will assume you're using the following UIWebView method:

要解决这个问题,您需要手动提供一个基于文档的url。您还没有发布如何以编程方式加载索引的代码。html文件,假设你使用的是下面的UIWebView方法

- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL

So now try out this code, see if it solves your problem:

现在试试这段代码,看看它能不能解决你的问题:

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

If that doesn't work, try updating the code of the HTML file that you get from the .zip file. Something like:

如果不行,请尝试更新从.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


3  

May be your JS And CSS File are not compiled by xcode. Open your project with xcode and GO to 'Targets' and check in the 'Compile Sources' section . If your js file is present in that folder, move it to the 'Copy Bundle Resource' and delete is from "Compile Sources" folders.

可能是你的JS和CSS文件不是由xcode编译的。使用xcode打开项目,进入“target”并检查“Compile Sources”部分。如果您的js文件存在于该文件夹中,那么将其移到“Copy Bundle Resource”中,并从“Compile Sources”文件夹中删除。

After that delete app from your device, then Go to the Build menu in xcode and clean all Targets and recompile.

从设备中删除应用程序之后,进入xcode中的Build菜单,清除所有目标并重新编译。

Check to see if your HTML file including with your js file.

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

May be its your trouble for loading js.

可能是你加载js的麻烦。

#3


3  

try this ...

试试这个…

i hope this will solve you problem

我希望这能解决你的问题

I also faced the same problem , I solved it by doing the simple thing when adding the files to XCode I did a small change that is shown in the figure below: you have to add files by choosing create folder references for any folders option

我也遇到了同样的问题,我在向XCode添加文件时做了一件简单的事情,我做了一个小更改,如下图所示:您必须为任何文件夹选择create folder引用来添加文件

In the figure below one is for adding HTML , click copy also if you are adding file outside from project . if files are already present then no need to copy it again .

在下面的图中,一个是用于添加HTML的,如果您正在从项目外部添加文件,也请单击copy。如果文件已经存在,那么不需要再次复制它。

当从文档目录打开UIWebview时,ios-css和js没有链接到html文件中。

#1


4  

Apparently the document base is not the same as the app's Documents directory when loading the HTML file programatically.

显然,在程序化加载HTML文件时,文档库与应用程序的文档目录不同。

Check out the BASE element in HTML, it goes within the <head> element:

查看HTML中的基本元素,它进入元素:

http://www.w3.org/wiki/HTML/Elements/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>

To fix that, you'll have supply a Document base url manually. You haven't posted the code how you're programmatically loading the index.html file, so I will assume you're using the following UIWebView method:

要解决这个问题,您需要手动提供一个基于文档的url。您还没有发布如何以编程方式加载索引的代码。html文件,假设你使用的是下面的UIWebView方法

- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL

So now try out this code, see if it solves your problem:

现在试试这段代码,看看它能不能解决你的问题:

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

If that doesn't work, try updating the code of the HTML file that you get from the .zip file. Something like:

如果不行,请尝试更新从.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


3  

May be your JS And CSS File are not compiled by xcode. Open your project with xcode and GO to 'Targets' and check in the 'Compile Sources' section . If your js file is present in that folder, move it to the 'Copy Bundle Resource' and delete is from "Compile Sources" folders.

可能是你的JS和CSS文件不是由xcode编译的。使用xcode打开项目,进入“target”并检查“Compile Sources”部分。如果您的js文件存在于该文件夹中,那么将其移到“Copy Bundle Resource”中,并从“Compile Sources”文件夹中删除。

After that delete app from your device, then Go to the Build menu in xcode and clean all Targets and recompile.

从设备中删除应用程序之后,进入xcode中的Build菜单,清除所有目标并重新编译。

Check to see if your HTML file including with your js file.

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

May be its your trouble for loading js.

可能是你加载js的麻烦。

#3


3  

try this ...

试试这个…

i hope this will solve you problem

我希望这能解决你的问题

I also faced the same problem , I solved it by doing the simple thing when adding the files to XCode I did a small change that is shown in the figure below: you have to add files by choosing create folder references for any folders option

我也遇到了同样的问题,我在向XCode添加文件时做了一件简单的事情,我做了一个小更改,如下图所示:您必须为任何文件夹选择create folder引用来添加文件

In the figure below one is for adding HTML , click copy also if you are adding file outside from project . if files are already present then no need to copy it again .

在下面的图中,一个是用于添加HTML的,如果您正在从项目外部添加文件,也请单击copy。如果文件已经存在,那么不需要再次复制它。

当从文档目录打开UIWebview时,ios-css和js没有链接到html文件中。