如何用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]];
这是你的一个方法。 从你说的上下文,你有源文件js
和css
。
这里的问题可能是因为运行时间这些文件没有加载或编译器无法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>
所以你必须手动提供这些文件的位置。
在这里查看标签href
和src
。 这些是资源文件的位置,当您的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是否正确。
- AlertController中的Html文件?
- iphone应用程序和uiwebview与JavaScript代码可以访问variables?
- iOS8 Safari浏览器-webkit-overflow-scrolling:touch; 问题
- 如何在UIWebview中的目录(JS,图像,CSS)加载本地HTML资产? (IOS)
- HTML正文中的超链接iOS SMSscheme
- 如何通过htmlselect标签创buildUIPickerView的对象引用
- 如何在UIWebView中保存内容以便在下次启动时加载更快?
- 我可以如何使网页浏览器的本机应用程序的URL与url后备?
- 如何处理JSON提供的HTML文本格式的UITextView