A
A
Alexander Vasyuchenko2014-02-20 12:52:27
Objective-C
Alexander Vasyuchenko, 2014-02-20 12:52:27

UIWebView: how to catch page load event on ajax sites?

Good afternoon!
Please tell me how to catch the page loading event on ajax sites when the delegate methods:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

- (void)webViewDidFinishLoad:(UIWebView *)webView
are not called. Checked on m.vk.com.
I need this to block and unblock the back/forward buttons while navigating through pages.
Google prompted to dig in the direction of NSURLProtocol, but so far it is difficult to determine the transition to all m.vk.com links

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
max_sokolov, 2014-02-20
@max_sokolov

If the buttons are always active, is there a problem with navigation? Methods goBack goForward fulfill?
In any case, NSURLProtocol can help. Here's a no-frills example . On the didReceiveResponse event, you can check canGoBack and canGoForward Google a
little on the topic, the guys on stackoverflow.com still have problems with html5 cache on iOS 7.

A
Alexander Vasyuchenko, 2014-02-27
@alexv1981

Hmm ... and when loading this page into the browser using my protocol, in general, the brakes and freezes are considerable. It seems to do everything like here :

@implementation WebBrowserURLProtocol
{
    NSURLConnection *_connection;
}

#pragma mark - NSURLProtocol

+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
    if ([NSURLProtocol propertyForKey:@"urlConnectionSent" inRequest:request] != nil)
        return NO;
    
    return YES;
}

+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
{
    return request;
}

- (void)startLoading
{
    NSMutableURLRequest *newRequest = [self.request mutableCopy];
    
    [NSURLProtocol setProperty:@YES forKey:@"urlConnectionSent" inRequest:newRequest];
    
    _connection = [NSURLConnection connectionWithRequest:newRequest delegate:self];
}

- (void)stopLoading
{
    [_connection cancel];
}

#pragma mark - NSURLConnectionDelegate

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.client URLProtocol:self didLoadData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [self.client URLProtocol:self didFailWithError:error];
    _connection = nil;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageAllowed];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [self.client URLProtocolDidFinishLoading:self];
    _connection = nil;
    
    [WebBrowserURLProtocol postNotification];
}

#pragma mark - Notifications

+(void)postNotification
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"kURLProtocolDidFinishLoading" object:nil];
    });
}

@end

As a result, I rewrote the class using only one method in it
+ (BOOL)canInitWithRequest:(NSURLRequest *)request
and the brakes were gone. For the task of updating the back/forward buttons, this is quite tolerable:
@implementation WebBrowserURLProtocol

+(void)postNotification
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"kURLProtocolDidFinishLoading" object:nil];
    });
}

+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
    [WebBrowserURLProtocol postNotification];
    
    return NO;
}

+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
{
    return request;
}

- (void)startLoading
{
}

- (void)stopLoading
{
}

@end

from what such troubles with this class....?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question