uriel
uriel

Reputation: 1248

How to listen HTTP requests sent by WebView (objective c)?

I'm trying to make an event which will be called (and will excecute objective-c code on iPhone) when specific button is pressed on a website in webView. The simple way I think is to listen webView's HTTP requests. Can I do that?

Upvotes: 1

Views: 699

Answers (1)

Rob Napier
Rob Napier

Reputation: 299545

In your HTML, give the URL a special scheme. In this example, the scheme is perform:

<!-- ontouchstart tells WebKit to send us mouse events on a touch platform so we can use :active -->
<button class="button" ontouchstart="" onclick="window.open('perform:MAX')">MAX</button>

(You could use <a href here or other techniques. This example comes from code where using onclick was useful.)

Set your controller as the delegate of the UIWebView. Then implement this delegate method:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request 
                                                 navigationType:(UIWebViewNavigationType)navigationType
{
  NSURL *url = request.URL;
  if ([[url scheme] isEqualToString:@"perform"])
  {
    // url.resourceSpecifier will be @"MAX" in this example
    // Do something with it.
    return NO;
  }

  return YES;
}

Upvotes: 3

Related Questions