【问题标题】:Trigger an HTML button click through objective-c?通过objective-c触发HTML按钮点击?
【发布时间】:2013-03-21 02:34:24
【问题描述】:

我的目标是向 Web 服务器发送 HTTP 请求,并为其提供用户名和密码。我把那部分记下来了,它出人意料地奏效了。但是,要真正登录,必须单击“登录”按钮。这把我彻底难住了??

NSLog(@"Connection started");
        NSURL *loginPage = [NSURL URLWithString:@"www.A_WEBSITE.com"];
        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:loginPage];
        [request setRequestMethod:@"POST"];
        [request setPostValue:@"Username" forKey:@"ct100_plnMain_txtLogin"];
        [request setPostValue:@"Password" forKey:@"ct100_plnMain_txtPassword"];

        [request start];

        NSLog(@"it worked");
        [request setDidFailSelector:@selector(AttemptFail:)];
        [request setDidFinishSelector:@selector(AttemptSucceed)];



        NSError *error = [request error];
        if (!error) {
            NSString *response = [request responseString];
            NSLog(@"%@",response);
                    }

            else {
                NSLog(@"%@",[request error]);
            }

如果我有它的 ID,有谁知道我可以触发按钮上的点击事件的方法吗?

【问题讨论】:

  • UIWebView 中渲染页面,然后使用-[UIWebView stringByEvaluatingJavaScriptFromString:] 在页面上执行一些javascript...
  • 为什么要设计 HTML 页面,只需设计一个包含 2 个文本字段和一个按钮的登录屏幕,然后单击该按钮调用此方法

标签: iphone ios objective-c web network-programming


【解决方案1】:

使用js发送请求并通过:

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

【讨论】:

  • 原谅我,对于 iOS 的整个 Web 端,我是个菜鸟。我将如何在我的代码中实现它??
  • 它是一个委托方法,只需设置 web 视图的委托 = yr 控制器并实现此方法。在这个方法中,你需要比较请求来检查它是否是你设置的字符串。如果是,请调用一些本机方法。顺便说一句,@Vinodh 的回答看起来很棒而且很完整^^
【解决方案2】:
[UIWebView stringByEvaluatingJavaScriptFromString:@"functionName()"];

<script>标签内的html文件中写入functionName()方法并执行按钮单击的动作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-06
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    相关资源
    最近更新 更多