如何用CSS,JS和图像调用HTML文件?

我正在从服务器下载zip文件。 在NSCachesDirectory我有HTML文件,我想知道如何运行HTML文件与他们的源文件,我已经粘贴我的代码。 请帮帮我

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *documentsPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"/Lesson-file/Lesson%d",[str_lsn_id integerValue]]]; NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"story_html5" ofType:@"html" inDirectory:documentsPath]]; [lsn_play_webview loadRequest:[NSURLRequest requestWithURL:url]]; 

这是你的一个方法。 从你说的上下文,你有源文件jscss

这里的问题可能是因为运行时间这些文件没有加载或编译器无法find它们的位置。

例如:

  <!--For CSS file html tag is--> <link rel="stylesheet" href="styles.css"> <!--For JS file html tag is--> <script type="text/javascript" src="exmample.js"></script> 

所以你必须手动提供这些文件的位置。

在这里查看标签hrefsrc 。 这些是资源文件的位置,当您的html页面加载到内存中时,这些资源文件将在运行时需要。

如果这些文件在同一个目录中,则不需要提供url

build议:

build议您在这里提供文件位置url。 编辑您的html文件并为url标记添加标记。

示例代码:

 <html> <head> <script type="text/javascript" src="#JS-FILE1#"></script> <link rel="stylesheet" href="#CSS-FILE1#"> </head> <body> </body> </html> 

你可以看到我用标记#JS-FILE1##CSS-FILE1#replace了文件。 现在我们将在加载html文件之前用实际的URLreplace这些标记。

 NSString *path = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"html"]; NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; // Location of Resource files NSString *jsFilePath = [[NSBundle mainBundle] pathForResource:@"exmample" ofType:@"js"]; NSString *cssFilePath = [[NSBundle mainBundle] pathForResource:@"stylesheet" ofType:@"css"]; html = [html stringByReplacingOccurrencesOfString:@"#JS-FILE1#" withString:jsFilePath]; html = [html stringByReplacingOccurrencesOfString:@"#CSS-FILE1#" withString:cssFilePath]; 

把这里放在断点处并debugging输出string。 NSLog这个string复制它并创build临时文件 – 在浏览器中打开它来检查所有的资源是否加载?

如果没有,那么检查资源文件的URL是否正确。