你可以在UIWebView中拦截NSURLRequests而不中断后退按钮? [英] Can you intercept NSURLRequests in a UIWebView without breaking the back button?

查看:246
本文介绍了你可以在UIWebView中拦截NSURLRequests而不中断后退按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将自定义HTML加载到我的 UIWebView 而无需破坏其 goBack 方法。



效果



我拦截了我的 UIWebView ,所以我可以加载自定义HTML。我可以控制所有的HTML,所以我有我的特殊的应用程序请求使用自定义方案(即 myapp:// arg1 /?arg2 = val )在 webView:shouldStartLoadWithRequest:navigationType:。我决定什么HTML我真的想加载,并调用 loadHTMLString:baseURL 并返回 NO 取消原始请求。 / p>

无效的工作



问题是我想使用 UIWebView的 goBack 方法和 loadRequest: / code>似乎是添加到其历史堆栈的唯一 UIWebView 方法。



一些想法,但我不知道哪些是可行的,如何去他们。主要的事情似乎是,我必须返回 YES webView:shouldStartLoadWithRequest:navigationType ,我必须使用 UIWebView loadRequest 方法。



1:修改NSURLRequest / Response:
我可以继承NSURLRequest,以便(当UIWebView发出请求时)它实际上不会发出HTTP请求并返回一个带有我的HTML的NSURLResponse?或者可能修改/子类/添加类别方法NSURLResponse某种方式?我喜欢这是一个真正的请求,但我关心的私人API和被拒绝从App Store。



想法2:处理自定义URL协议
注册自定义URL协议,以便我的应用程序对其进行响应,并且可以返回合法的NSURLResponse(用自定义HTML填充)。



想法3:刷新缓存使用此缓存策略 NSURLRequestReturnCacheDataDontLoad 创建请求,然后以某种方式在webView和

解决方案



我建议不要使用上述任何方法。



我有#3工作,但它非常,非常脆弱,很难调试。 (例如,Apple销毁并重新创建 NSURLRequests ,因此您不能只是子类化NSURLRequest,并期望在后续响应中通过。)



它最终对我自己的后台历史记录更加容易,并记录了要加载的页面和滚动位置(垂直屏幕偏移)。


I'm having trouble loading custom HTML into my UIWebView without breaking its goBack method.

What Works

I'm intercepting the URL requests of my UIWebView so I can load custom HTML. I have control over all the HTML, so I have my special app requests use a custom scheme (ie. myapp://arg1/?arg2=val) that I can parse in webView:shouldStartLoadWithRequest:navigationType:. I decide what HTML I really want to load and call loadHTMLString:baseURL and return NO to cancel the original request.

What Doesn't Work

The above works great. The problem is that I want to make use of the UIWebView's goBack method and loadRequest: appears to be the only UIWebView method that adds to its history stack.

I have a few ideas, but I'm not sure which are feasible and how to go about them. The main thing seems to be that I have to return YES in webView:shouldStartLoadWithRequest:navigationType and I have to use UIWebView's loadRequest method.

Idea 1: Modify NSURLRequest/Response: Can I subclass NSURLRequest so that (when the UIWebView makes the request) it doesn't actually make an HTTP request and returns an NSURLResponse with my HTML in it? Or maybe modify/subclass/add a category method to NSURLResponse somehow? I like the idea of it being a real request, but I'm concerned about private APIs and being rejected from the App Store.

Idea 2: Handle a custom URL Protocol Register a custom URL protocol so my app responds to it and I can have it return a legitimate NSURLResponse (filled with my custom HTML.)

Idea 3: Fool the cache Create the request with this cache policy NSURLRequestReturnCacheDataDontLoad and then somehow get my HTML in between the webView and the cache?

Or maybe I'm on the wrong track completely?

解决方案

I would recommend against any of the above approaches.

I got #3 working, but it was very, very fragile and hard to debug. (For example, Apple destroys and re-creates NSURLRequests, so you can't just subclass NSURLRequest and expect to come through in the subsequent response.)

It ended up being (much) easier to my own back history and note what page to load and scroll position (vertical screen offset).

这篇关于你可以在UIWebView中拦截NSURLRequests而不中断后退按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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