0

So, it is pretty clear question I think. This question is about monitoring ajax requests. I tried it but didn't work as I wanted.

How can I call a function in my native iOS Obj-C application every time a request is sent, received and the browsing link is changed in WKWebView?

Ali Barış Uzuner
  • 147
  • 1
  • 1
  • 10

1 Answers1

1

Ok, I found a way.

You can create a new class (let's call it MyURLProtocol) which has NSURLProtocol as subclass. Then add this function to MyURLProtocol:

+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
    NSLog(@"URL = %@", request.URL.absoluteString);
    return NO;
}

This function will be called each time your webview makes a request. And then you need to register this protocol with the loading system. In your Appdelegate.m file include your class and add/replace didFinishLaunchingWithOptions function with this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [NSURLProtocol registerClass:[MyURLProtocol class]];
    return YES;
}

All set. Now you can edit canInitWithRequest function and do what you want with the request.

Ali Barış Uzuner
  • 147
  • 1
  • 1
  • 10