iPad将本地HTML加载到带有图像和JavaScript的Web视图中 [英] iPad loading local HTML into web view with images and javascript

查看:129
本文介绍了iPad将本地HTML加载到带有图像和JavaScript的Web视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:



所以答案就在这篇文章中:
链接到WebView中的资源 - iPhone

解决方案

的确,您可以在 UIWebView 通过以下方式:

   -  loadHTMLString:baseURL:

你唯一应该注意的是如何指定 baseURL 以便它会转到您的本地资源目录。因此,如果您添加HTML:

 < img src =localImage.png/> 
< link rel =stylesheettype =text / csshref =theme.css/>
< script type =text / javascriptsrc =filename.js>< / script>

这将在您的项目资源中找到。



绝对有效。可能存在HTML和javascript源的一些问题(例如,使用子目录或绝对URL)。



编辑:



如果您需要在应用程序的Resources目录中的目录中组织资源(图像和脚本),请参阅 SO主题



要获取资源目录的路径,请使用:

  NSString * filePath = [[NSBundle mainBundle] pathForResource:@indexofType:@html]; 

另一种方法是将您的所有文件托管在 Documents 您的应用程序的目录(为用户内容保留)。在这种情况下,我想您可以下载应用程序的存档并将其安装在文档目录中。


Edit:

So the answer was in this post: Loading javascript into a UIWebView from resources

You'll find everything you need to know for loading an HTML5 app with directories there, just make sure you drop files on your Xcode project clicking the radio button that reads "Create folder references for any added folders". Then just use the code on that links I've added up there.

Thanks to Sergio for the help.


I've been through StackOverflow several times already and none of the code I find there can display the images of my html5 App. The hard part seems to be loading files from different subdirectories. I do not want to throw everything in the root and then use the groups in Xcode because I would have to re-factor a lot of the HTML5 code.

I'd like to build this in as a light container, not have to use PhoneGap. Besides PhoneGap comes with a bigger bagage of bugs (I tested our app and it crashed on the iPad and worked on the iPhone). It's hard enough to deal with the HTML5 side of things and Cocoa Touch.

So here's how I load the HTML page:

[webView loadRequest:[NSURLRequest requestWithURL:
       [NSURL fileURLWithPath:[[NSBundle mainBundle] 
       pathForResource:@"index" ofType:@"html"]isDirectory:NO]]]; 

I have found some solution that it's getting me closer to "Create folder references for any added folders" when adding the files to the resources folder, I can now see some of the images being loaded. The JS doesn't seem to be working properly. I have tested this app loading it from a URL running on a local server and it does work in that case.

So CSS is obviously working, images too but JS seems to be an issue.

Hopefully I'll find the answer soon.


Old

(this is when I was including the folders the wrong way)

So as you guys don't have to be wondering what am I seeing ( it looks to me like js and images are not loading and I do have subdirectories on the HTML5 app ):


Interesting posts which are related to this:

Loading javascript into a UIWebView from resources Link to resources inside WebView - iPhone

解决方案

Indeed, you can load a local HTML in a UIWebView by means of:

– loadHTMLString:baseURL:

The only thing you should be aware of is how to specify the baseURL so that it goes to your local resources directory. So, if you add in your HTML:

<img src="localImage.png" />
<link rel="stylesheet" type="text/css" href="theme.css" />
<script type="text/javascript" src="filename.js"></script>

this will be found among your project resources.

It definitely works. Possibly there is some "issue" with your HTML and javascript source (like, e.g., using subdirectories or absolute URLs).

EDIT:

If you need to organize your resources (images and scripts) in directories inside of the Resources directory of your app, see this S.O. topic.

To get the path to your Resources directory, use:

 NSString* filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];  

An alternative could be hosting all of your file in the Documents directory of your app (which is reserved for user content). In this case, I suppose you could download an archive of your app and install it in the document directory.

这篇关于iPad将本地HTML加载到带有图像和JavaScript的Web视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆