iPad loading local HTML into web view with images and javascript

心不动则不痛 提交于 2019-12-04 07:20:56
sergio

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!