UIWebView - 使用链接的资源加载本地.html文件 [英] UIWebView - Load a local .html file with linked resources

查看:104
本文介绍了UIWebView - 使用链接的资源加载本地.html文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我已经阅读过多少个论坛,但我真的不知道为什么它不起作用!



我有一个iPhone应用程序,我想向我的用户展示一本Epub书。

用户可以在线阅读,否则他可以将其下载到Application Documents目录中以便后续阅读。

我构建与在线版本相同的文件夹结构并将其保存到文档文件夹前端中。

我也可以阅读这个维护.HTML文件,但链接的JS / CSS和HTML文件不起作用。



所以我有一些截图。我不知道,为什么javascript无法访问.html文档。



离线版本 - 保存到文件系统文档文件夹中



网络版 - 直接从服务器 - 没关系!



我希望你能给我一些提示。

解决方案

经过大量的解答和阅读大量答案后,以下是我的简单整合解决方案:

1)确保你的所有包资源都是唯一的 - 目录变得扁平化 p 这太令人沮丧了,但如果您有目录About / index.html和Home / index.html,则最后一个index.html中的一个将被覆盖到前一个。



2)JavaScript文件将存在错误的构建阶段

在Xcode中单击您的项目,然后单击你的应用目标,然后点击Build Phase。注意,JavaScript文件将错误地位于Compiled Sources中:将JS文件移动到Copy Bundle Resources。


$ b

3)使用UIWebView的loadRequest



不要从字符串中加载。改为使用NSURL。

  NSBundle * mainBundle = [NSBundle mainBundle]; 
NSURL * url = [mainBundle URLForResource:@some_pagewithExtension:@html];
NSURLRequest * urlReq = [NSURLRequest requestWithURL:url];

//假设您创建一个定义如下的IBOutlet:
// @property(strong,nonatomic)IBOutlet UIWebView * wv;
[self.wv loadRequest:urlReq];


I don't know how many forums I have already read, but I really don't know why it doesn't work!

I have an iPhone Application and I would like to show a Epub book to my user.

The user can read it online, otherwise he can download it into the Application Documents directory to read it afterwards.

I build this folder structure same like the online version and saved it into the document folder "frontend".

I ALSO can read this maintaine .HTML file, BUT the linked JS / CSS and HTML files does not work.

So I have some screenshots. I don't know, why the javascript cannot access to the .html docs.

offline version - saved into the filesystem "document folder"

online version - directly from server - it's ok!

I hope you could give me some hints.

解决方案

After much fiddling around and reading a lot of answers, here is my simple consolidated solution:

1) Make sure all your bundle resources are unique---directories get flattened

It is super frustrating, but if you had directories "About/index.html" and "Home/index.html", one of the last "index.html" to get copied into the bundle will overwrite the previous.

2) JavaScript files will be in the wrong Build Phase

In Xcode click on your project, then click on your app target, then click on Build Phases. Notice the JavaScript files will wrongly be in Compiled Sources: move JS files into Copy Bundle Resources.

3) Use UIWebView's loadRequest:

Don't load from a string. Use an NSURL instead.

NSBundle *mainBundle = [NSBundle mainBundle];
NSURL *url = [mainBundle URLForResource:@"some_page" withExtension:@"html"];
NSURLRequest *urlReq = [NSURLRequest requestWithURL:url];

// Assuming you create an IBOutlet defined like:
// @property (strong, nonatomic) IBOutlet UIWebView *wv;
[self.wv loadRequest:urlReq];

这篇关于UIWebView - 使用链接的资源加载本地.html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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