与 [UIWebView loadRequest:] - 优化从 iAD 返回到具有 UIWebView 的视图
我有一个包含 UIWebView 和 iAD AdBannerView 的视图。
为了优化体验并减少带宽争用 - 我在加载 iAd“详细视图”时暂停 UIWebView 的网络活动,并在用户从广告返回时恢复它。目前,我只是执行以下操作:
-(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
if (self.webView.loading) {
[self.webView stopLoading];
self.loadingWasInterrupted = TRUE;
}
return YES;
}
-(void)bannerViewActionDidFinish:(ADBannerView *)banner
{
if (self.loadingWasInterrupted) {
//Should use loadRequest: instead?
[self.webView reload];
}
}
我试图了解第二次调用 reload 与 loadRequest: 之间是否有任何区别,如果有的话,哪个更有效。
我猜重新加载只是让您不必保留请求对象,并且确实做了同样的事情,但我想确定。文档和标题没有提供任何线索。
我知道我可以拆开网络活动来了解正在发生的情况,但希望之前看过此内容或通常了解重新加载行为是否与 loadRequest 完全不同的人提供任何见解。谢谢。
I have a view that includes a UIWebView as well as an iAD AdBannerView.
To optimize the experience and reduce bandwidth contention - I suspend the UIWebView's network activity when the iAd "detail view" is being loaded and resume it when the user returns from the ad. Currently, I simply do the following:
-(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
if (self.webView.loading) {
[self.webView stopLoading];
self.loadingWasInterrupted = TRUE;
}
return YES;
}
-(void)bannerViewActionDidFinish:(ADBannerView *)banner
{
if (self.loadingWasInterrupted) {
//Should use loadRequest: instead?
[self.webView reload];
}
}
I'm trying to understand if it there is any difference between calling reload vs. loadRequest: a second time, and if so, which is more efficient.
I'm guessing reload simply just saves you having to hold onto the request object and really does the same thing but I'd like to know for sure. The docs and header don't offer any clue.
I understand I could pick apart the network activity to understand what's happening but would appreciate any insight from someone who has looked at this before or who generally understands if reload behavior differs from loadRequest at all. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,这是一个相当复杂的解决方案,但我认为可能对您有用:
为 http 定义一个自定义协议处理程序:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html#//apple_ref/doc/uid/10000165i
使用 NSURLProtocol 作为子类。处理程序将启动 NSURLConnection,并在数据传入客户端时返回数据(在本例中,这将是启动连接的 UIWebView)。让它将自己添加为 NSNotificationCenter 的观察者以获取“暂停”通知。
当您想要显示 iAd 时,您可以发送暂停通知,这将取消 NSURLConnection(但不会取消 NSURLProtocol,它将保持打开和加载状态,因此您的 webview 将继续显示为正在加载)。
然后,当添加完成后,您可以发送“恢复”通知(大致相同),但在这种情况下,接收通知的任何活动 NSURLProtocol 处理程序将创建新的 NSURLConnections,使用 Range: 标头从中断处恢复:
iphone sdk:暂停 NSURLConnection?
一些注意事项:
仅在浏览支持恢复标头的网站时将起作用(否则您可能必须重新启动连接,并且忽略接收到的最新字节之前的数据)。
您的 NSURLRequests 应该形成,以便它们没有超时。 (如果你想要超时那么它应该在 NSURLProtocol 处理程序 NSURLConnections 中)。
Okay, a fairly complicated solution but never the less one that I think might work for you:
Define a custom protocol handler for http:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html#//apple_ref/doc/uid/10000165i
Using NSURLProtocol as the subclass. The handler will start a NSURLConnection, and return the data as it comes in to the client (in this case this will be the UIWebView that initiated the connection). Have it add itself as an observer to NSNotificationCenter for a "Pause" notification.
When you would like to display an iAd, you can send your pause notification, this will cancel the NSURLConnection (but not the NSURLProtocol, which will remain open and loading, and thus your webview will continue to appear as if it were loading).
Then when the add is finished you can send a "resume" notification (much the same), except in this case any active NSURLProtocol handlers receiving the notification will create new NSURLConnections, using the Range: header to resume where they left off:
iphone sdk: pausing NSURLConnection?
Some caveats:
Only will work when browsing websites that support the resume header (otherwise you might have to start the connection anew, and just ignore data prior to the latest byte received).
Your NSURLRequests should be formed so that they don't have a timeout. (if you want a timeout then it should be in the NSURLProtocol handlers NSURLConnections).
我在这里猜测,但我相信重新加载正在内部执行 loadRequest: 。如果你真的想测试这个,你可以冷添加一个临时子类到 UIWebView 并重写 reload 和 loadRequest 方法只是为了记录日志。
I'm guessing here, but I believe the reload is doing a loadRequest: internally. If you are really intent on testing this you cold add a temporary subclass to UIWebView and override the reload and loadRequest methods just for the sake of logging.
当你调用方法“loadRequest”时,你可以像这样调用它,
但是当你调用“reload”时,你只是指示重新加载当前请求是什么。
所以基本上在后一种情况下,您可以避免从字符串创建 url,然后从 url 创建请求,这使得使用起来非常方便。
根据功能的情况,reload 本身会调用 loadRequest,因此基本上在效率甚至速度方面没有差异。
然而,基本上对于你的情况和我的许多情况来说,我们想要但苹果没有给我们的东西是这样的: -
所以总结整个事情,使用它们中的任何一个,但如果你像我一样懒惰再次指定urlrequest 只需使用“reload”
道歉
抱歉,我以前认为重新加载必须调用 loadRequest,但是当我尝试 NWCoder 的方法时,它不会在调用重新加载时调用 loadRequest。但我仍然认为 reload 和 loadRquest 遵循相同的方法加载页面
When you call the method "loadRequest", you call it like
But when u call "reload", u just instruct to reload whatever the current request is.
So basically in latter case , u are saving urself from creating a url from string and then a request from url and that makes it pretty convenient for use.
As per case of functionality, reload itself calls loadRequest so basically there is no difference in terms of efficiency and even in speed.
However basically for ur case and in many of my cases , the thing which we want but Apple has not given us is something like:-
So to sum up the whole thing , use any of them but if u r lazy like me to again specify the urlrequest just use "reload"
Apologies
Sorry guys I used to think that reload must be calling loadRequest but when I tried NWCoder's method, it doesnot call loadRequest on calling reload. But I still think reload and loadRquest follows the same method for loading the page