1

I have a UIWebView page with multiple button . Need to identify which button is clicked of the UIWebView.

Page source for UIWebview load contents:

        <form action="settings_personal.php" data-ajax="false" method="post">   
            <input type="submit" name ="btn_settings_personal" value=" Goals (Personal data) " data-icon="star" data-theme="g" />
        </form> 

        <form action="settings_global.php" data-ajax="false" method="post"> 
            <input type="submit" name ="btn_settings_global" value=" Settings " data-icon="gear" data-theme="g" />
        </form>
        <form action="" data-ajax="false" method="post">    
            <input type="submit" name ="btn_web_access" value=" Web Plattform Access " data-icon="plus" data-theme="h" />
        </form>     

I need to identify button clicked for 'btn_web_access' named button ???

I have implemented following :

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{

 NSString* clicked = [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('btn_web_access').click();"];

    NSLog(@"CLICK %@",clicked);

}

but its of no use..Thanks in advance.

Deepesh
  • 8,065
  • 3
  • 28
  • 45
Injar
  • 125
  • 1
  • 4
  • 12
  • Your stringByEvaluatingJavaScriptFromString call will just trigger a new button click event. That does not seem useful, as you are asking on how to identify which button has been clicked, not how to click a button programmatically. See my answer on wether it helps you. – alex Aug 10 '12 at 08:14

2 Answers2

5

Just use the request's URL attribute to distinguish the load request's target:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{

    NSLog(@"url: %@", [[request URL] absoluteString]);

    return YES;

}

Based on the URL being called, you know which button (or even any link) of your currently shown webpage was clicked. Then take the action being needed and don't forget to return a BOOL value indicating wether or not to actually load the requested URL.

In Swift you might have

func webView(webView:UIWebView,
          shouldStartLoadWithRequest request:NSURLRequest,
          navigationType:UIWebViewNavigationType) -> Bool
    {
    if (navigationType == .FormSubmitted)
        {
        print("was the 'submit' form")
        }

    let u = request.mainDocumentURL
    print("local or remote url was " ,u)

    return true // allow the form to in fact send the form
    }
Fattie
  • 27,874
  • 70
  • 431
  • 719
alex
  • 2,464
  • 23
  • 32
  • But Alex webpage is not containg any action , pls check the below
    – Injar Aug 10 '12 at 08:15
  • 1
    Just set any arbitrary link, which will help you identify your button, inside the action parameter. When a user clicks this button, just return a NO in shouldStartLoadWithRequest. So this click will not result in an actual page load inside the webview. – alex Aug 10 '12 at 08:19
  • Alex due you any other option like to identify Clicked button's name of the UIWebview. – Injar Aug 10 '12 at 08:49
  • Another approach would be similar: 1) Inside HTML, don't submit the form directly on button click. Instead, call a javascript function, that saves a button identifier inside a global variable **and** submits the form then, if necessary. 2) Get the current javascript button identifier value inside shouldStartLoadWithRequest method by stringByEvaluatingJavaScriptFromString. But you still need an action parameter set to every form. – alex Aug 10 '12 at 09:37
  • Alex i got another option to get the Value of the UIWebview button i.e the name which will be easier to identify...BY below code - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType { NSString* value1 = [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('btn_web_access')[0].value"]; NSLog(@"Uersname : %@",value1); } – Injar Aug 10 '12 at 11:42
0

Do this with HTML Code

- (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;
}
JamesT
  • 2,988
  • 23
  • 29
Vikas Rajput
  • 1,754
  • 1
  • 14
  • 26