确定UIWebview的提交按钮单击

时间:2012-08-10 07:09:46

标签: iphone uiwebview uibutton getelementbyid

我有一个带有多个按钮的UIWebView页面。需要确定点击UIWebView.

的按钮

UIWebview加载内容的页面源:

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

我需要识别点击“btn_web_access”命名按钮???

的按钮

我已实施以下内容:

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

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

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

}

但它毫无用处......谢谢。

2 个答案:

答案 0 :(得分:4)

只需使用请求的URL属性来区分加载请求的目标:

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

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

    return YES;

}

根据被调用的网址,您知道当前显示的网页的哪个按钮(甚至任何链接)已被点击。然后采取所需的操作,不要忘记返回一个BOOL值,表示是否实际加载请求的URL。

在Swift中你可能有

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
    }

答案 1 :(得分:0)

使用HTML代码执行此操作

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