How to Intercept Button Click Inside Uiwebview on Ios

how to intercept Button click inside UIWebview on IOS?

You can do the following:

In your HTML

<a class="yourButton" href="inapp://capture">Button Text</a>

In your UIWebViewDelegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([request.URL.scheme isEqualToString:@"inapp"]) {
if ([request.URL.host isEqualToString:@"capture"]) {
// do capture action
}
return NO;
}
return YES;
}

I added "capture" to your button's URL so that you could distinguish between several buttons that should trigger different actions (if you want to):

iOS UIWebView intercept link click

Yes, look at the UIWebViewDelegate Protocol Reference, specifically webView:shouldStartLoadWithRequest:navigationType:

The NSURLRequest will let you know where the link they clicked is going to direct them.

You would simply return NO on this method if you want to prevent the request from loading.

Can you intercept NSURLRequests in a UIWebView without breaking the back button?

I would recommend against any of the above approaches.

I got #3 working, but it was very, very fragile and hard to debug. (For example, Apple destroys and re-creates NSURLRequests, so you can't just subclass NSURLRequest and expect to come through in the subsequent response.)

It ended up being (much) easier to my own back history and note what page to load and scroll position (vertical screen offset).

Clicking a link in UIWebView pushes onto the NavigationView stack

This question has been asked many times before. Please have a look at this page, it's almost exactly what you need, just change the [BrowserViewController openBrowserWithUrl:url]; to whatever you need to push your new ViewController to the navigation stack. It might look like that:

LatestView *latestView = [[LatestView alloc] initWithNibName:@"LatestView" bundle:nil]; 

//Pass the URL here, depends on how you passed it into the WebView in the first place

// Add the ViewController to the stack
[self.navigationController pushViewController:latestView animated:YES];
[latestView release];


Related Topics



Leave a reply



Submit