A
A
Alexander Vasyuchenko2014-02-24 14:30:00
Objective-C
Alexander Vasyuchenko, 2014-02-24 14:30:00

UIWebView: how to keep url history on ajax sites?

Good afternoon!
Previously , the question arose of how to update the browser's back/forward buttons when visiting pages of ajax sites (such as m.vk.com). I partially solved this problem.
Now the task is how to keep a history of URLs for the back / forward buttons when it is problematic for ajax sites to identify the moment of transition and the page URL itself.
This is done in mobile safari. How to implement this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2014-02-24
@alexyat

NSURLProtocol to help you, eng.42go.com/customizing-uiwebview-requests-with-n... sees all http requests.
For example, I execute my preparation for mp3 detection in order to register a class. this code
MP3DetectURLProtocol.h

#import <Foundation/Foundation.h>

@interface MP3DetectURLProtocol : NSURLProtocol

@property (nonatomic, strong) NSURLConnection *connection;

@end

MP3DetectURLProtocol.m
#import "MP3DetectURLProtocol.h"

@implementation MP3DetectURLProtocol

static NSString *PBProxyURLHeader = @"X-PB";

+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
    //if([[request.URL absoluteString] rangeOfString:@"mp3"].length !=0)
        NSLog(@"request url = %@", [request.URL absoluteString]);
    if ([request valueForHTTPHeaderField:PBProxyURLHeader] == nil)
    {
        return YES;
    }
    return NO;
}

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

- (void) startLoading
{
    NSMutableURLRequest *newRequest = [self.request mutableCopy];
    [newRequest setValue:@"" forHTTPHeaderField:PBProxyURLHeader];
    // Here we set the User Agent
    //[newRequest setValue:@"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36 Kifi/1.0f" forHTTPHeaderField:@"User-Agent"];
    
    //[NSURLProtocol setProperty:@YES forKey:@"UserAgentSet" inRequest:newRequest];
    //NSLog(@"start loading");
    self.connection = [NSURLConnection connectionWithRequest:newRequest delegate:self];
}

- (void)stopLoading
{
    //[self.connection cancel];
}

- (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];
    self.connection = nil;
}

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

- (NSURLRequest *)connection:(NSURLConnection *)connection
             willSendRequest:(NSURLRequest *)request
            redirectResponse:(NSURLResponse *)response
{
    [self.client URLProtocol:self wasRedirectedToRequest:request redirectResponse:response];
    return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [self.client URLProtocolDidFinishLoading:self];
    self.connection = nil;
}

@end

M
max_sokolov, 2014-02-25
@max_sokolov

Interesting what tasks you have with UIWebView. Well, what's the option. Let's look at the stringByEvaluatingJavaScriptFromString method of UIWebView . In webViewDidFinishLoad , you can see which links were clicked, for example, like this
But there is a problem here stringByEvaluatingJavaScriptFromString injects the script and returns control. Accordingly, if we put return instead of alert, then we will get nothing in result . You can try to execute the script after some UITapGestureRecognizer .
As a result, I'm not sure, there is no time to try now, but as food for thought.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question