Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
59
rated 0 times [  65] [ 6]  / answers: 1 / hits: 41403  / 12 Years ago, tue, june 12, 2012, 12:00:00

I have a HTML form that has certain fields which i am opening inside a UIWebview. On click of a particular button i want to do a in app action.



The approach i'm using now is on click of the button i redirect the page to a dummy URL. I sniff the URL using delegate method shouldStartLoadWithRequest and stop the redirection. I then do my custom event (capture image and upload) and then continue. This seems to be an ugly hack. Anyway i can directly hook into the button click event rather than a page redirection?



UPDATE
There seems to be no way to setup a delegate method to fire when a JS function is called on button click. The only way to do this is to use the URL sniffing method mentioned above. This has been explained in detail by joern below, so I will accept his answer.


More From » iphone

 Answers
48

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):


[#84975] Sunday, June 10, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
joanneamiyaa

Total Points: 532
Total Questions: 127
Total Answers: 98

Location: Guam
Member since Tue, Nov 3, 2020
4 Years ago
;