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
Map, and Removing Commas from an Array in JavaScript
How to Get the Latest and Oldest Record in Mongoose.Js (Or Just the Timespan Between Them)
Text Input Allow Only Integer Input in Angularjs
Image Upload With Preview and Delete Option - Javascript/Jquery
Show 2 Items Per Row[React Native]
Bootstrap Datepicker Appearing At Incorrect Location in a Modal
How to Validate for Only CSV File Uploads Using the Pattern Attribute Using Html5
Progressive Web App - Service Worker Not Serving Start_Url
How to Capture the Browser Window Close Event
Easiest Way to Mask Characters in Html(5) Text Input
Modify Classname When Element Is Clicked in React
Basic Open Source JavaScript Image Editor
Getting Selected Values from Dropdown Inside Table
Vuejs Accessing a Method from Another Method
Regular Expression Which Matches a Pattern, or Is an Empty String