Identify submit button click of UIWebview

后端 未结 2 1252
一生所求
一生所求 2021-01-24 07:16

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

Page source for UIWebview load contents:

相关标签:
2条回答
  • 2021-01-24 07:51

    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
        }
    
    0 讨论(0)
  • 2021-01-24 07:55

    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;
    }
    
    0 讨论(0)
提交回复
热议问题