开发者

vs. [UIWebView loadRequest:] - optimizing return from iAD to a view with a UIWebView

I have a view that includes a UIWebView as well as an iAD AdBannerView.

To 开发者_Go百科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.


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).


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.

- (void)reload
{
    NSLog(@"reload called");
    [super reload];
}

- (void)loadRequest:(NSURLRequest *)request
{
    NSLog(@"loadRequest called");
    [super loadRequest:request];
}


When you call the method "loadRequest", you call it like

[webview loadRquest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.ururl.com"]]];

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:-

[webview pauseLoading];
[webview resumeLoading];

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜