确定UIWebview的提交按钮单击

Inj*_*jar 1 iphone getelementbyid uibutton uiwebview

我有一个带有多个button的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>     
Run Code Online (Sandbox Code Playgroud)

我需要确定为“ 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);

}
Run Code Online (Sandbox Code Playgroud)

但是没有用。。

ale*_*lex 5

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

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

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

    return YES;

}
Run Code Online (Sandbox Code Playgroud)

根据被调用的URL,您知道单击了当前显示的网页中的哪个按钮(甚至任何链接)。然后采取必要的操作,不要忘记返回一个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
    }
Run Code Online (Sandbox Code Playgroud)