简体   繁体   English

在UIWebView中使用自定义URL方案

[英]Using a custom URL scheme with UIWebView

I have a black box container. 我有一个黑匣子容器。 I love black boxes, they obfuscate things so well. 我喜欢黑匣子,它们使事物变得如此混乱。

This black box is an encrypted zip (sort of) and has inside some html files (this is the short, not so painful to explain, version). 这个黑匣子是一个加密的zip(某种),内部有一些html文件(这是简短的版本,解释起来并不麻烦)。

Those files need to be displayed in an UIWebView. 这些文件需要显示在UIWebView中。 Now, the easy way to do it, decrypt, unzip to filesystem, load file from filesystem. 现在,最简单的方法是解密,解压缩到文件系统,从文件系统加载文件。 That's good, except, the black box contains secret stuff, and can't just lay around on the filesystem, not even a sec, so, I made a C library that actually streams the contents of the box (directly out of the box). 很好,除了黑匣子包含秘密内容,而且不能只在文件系统中闲逛,甚至不花一秒钟的时间,所以,我制作了一个C库,实际上是在流传输箱子的内容(直接从箱子中取出) 。

Now, I have this streaming capability and have to somehow make it work with UIWebView. 现在,我具有此流功能,并且必须以某种方式使其与UIWebView一起使用。 First thing that comes in my mind would be to use a mini local HTTP server where the UIWebView can sent its requests. 我想到的第一件事是使用微型本地HTTP服务器,UIWebView可以在其中发送其请求。 I would then manage the requests myself and return the contents the UIWebView requires using the streaming lib I've done. 然后,我将自己管理请求,并使用我完成的流媒体库返回UIWebView所需的内容。 That would work I suppose well, but I think a mini HTTP server would somehow, maybe, be a little bit of a overkill. 我以为这可以正常工作,但是我认为微型HTTP服务器在某种程度上可能有点过大。

So, I was wondering, is there another way to interfere between UIWebView and the filesystem? 因此,我想知道,是否还有另一种方法可以在UIWebView和文件系统之间进行干预? Maybe using a custom schema? 也许使用自定义架构? Like myschema://? 像myschema://? And every time the UIWebView makes a request to myschema://myfile.html I would somehow interfere and return the data it needs? 而且,每次UIWebView向myschema://myfile.html发送请求时,我都会以某种方式干扰并返回所需的数据吗?

Is such a idea viable? 这样的想法可行吗? Where should I look to start from? 我应该从哪里开始? Maybe NSURLRequest? 也许NSURLRequest?

EDIT: I found this: iPhone SDK: Loading resources from custom URL scheme . 编辑:我发现了: iPhone SDK:从自定义URL方案加载资源 It sounds good, however, how will the browser know the size of the request, the type (xml/binary/xhtml) and all the info HTTP puts in its header? 听起来不错,但是,浏览器如何知道请求的大小,类型(xml / binary / xhtml)和所有信息HTTP放在其标头中?

Create a custom NSURLProtocol subclass and register it so it will handle the HTTP requests. 创建一个自定义NSURLProtocol子类并注册它,以便它将处理HTTP请求。 This will allow you to handle the requests that come from the UIWebView however you see fit, including supplying the data from your library. 这将使您能够处理来自UIWebView的请求,但您认为合适,包括从库中提供数据。 You can examine an implementation of one that performs disk caching of requests to allow offline browsing by looking at RNCachingURLProtocol . 您可以通过查看RNCachingURLProtocol来检查执行请求磁盘缓存以允许脱机浏览的实现 I personally use a custom NSURLProtocol subclass that I wrote to handle injecting some javascript code into pages that are loaded in the UIWebView , and it works very well. 我个人使用了一个自定义的NSURLProtocol子类,该子类是我编写的,用于将一些JavaScript代码注入到UIWebView中加载的页面中,并且效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM