Identify submit button click of UIWebview

耗尽温柔 提交于 2019-12-02 08:18:08

问题


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.


回答1:


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
    }



回答2:


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;
}


来源:https://stackoverflow.com/questions/11896985/identify-submit-button-click-of-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!