以编程方式在网站上键入字符串 [英] Type in String on Website programmatically

查看:50
本文介绍了以编程方式在网站上键入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何或是否可以从我的iPhone应用程序代码在网站的textField中键入内容.因此,我想去一个网站,该网站的中间是一个textField,在那我想输入一个特定的字符串.如何在Swift中做到这一点(或者在Objective-C中,我将弄清楚它如何在Swift中工作)?非常感谢您的帮助:)

I'd wanted to know how or whether I can type in something in a textField on a website from my iPhone application code. So I want to go to a website where is one textField in the middle and there I would like to type in a specific string. How can I do that in Swift (or Objective-C - I'll figure out how it works in Swift then)? I would really much appreciate any help :)

推荐答案

您可以从代理中注入javascript.例如,在Obj-C中(我还不知道迅捷程度如何):

You can inject javascript from your delegate. For instance, in Obj-C (I don't know swift good enough yet) :

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [_webView stringByEvaluatingJavaScriptFromString:@"doSomeStuff()"];
}

在您的情况下,您将需要操纵DOM在文本字段中添加一些文本-像这样:

In your case, you'll want to manipulate the DOM to add some text in your textfield - something like this :

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSString * javascript_code = @"document.querySelector('input[name=your_input_name]').value = 'Foobaz'";
    [_webView stringByEvaluatingJavaScriptFromString:javascript_code];
}

尽管要注意 querySelector ,但我不确定它是否可以在iOS的Webview中使用.

Beware of querySelector though, I'm not sure about its availability in iOS's webview.

这篇关于以编程方式在网站上键入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆