我正在开发一个 iOS 应用程序来扫描 vin 号码。我能够成功扫描 vin 号码。我希望将此 vin 号码传递给网站“http://tk14.webng.com/bswbhtml.html”。这里会有一个文本字段。在我单击应用程序中的按钮后,它应该会自动复制到文本字段。
代码如下
- (IBAction)sendVIN:(id)sender
{
_resultField.text = vin;
}
我在变量 vin
中得到了 vin 号。这必须自动复制到该文本字段。
我该怎么做?
最佳答案
您需要修改网页上输入字段的 html,添加一个“值”属性:
<input value="vin number goes here" />
您可以通过多种方式实现这一目标。最好的方法是通过 NSURLConnection 加载网页,而不是通过 webview 加载网页,修改 HTML,然后要求 webview 加载修改后的 HTML。这是一个例子:
int vinNumber = 1234567890;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://tk14.webng.com/bswbhtml.html"]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error == nil && data != nil)
{
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if ([html rangeOfString:@"<input type=\"text\" name=\"vinnumber\">"].location != NSNotFound)
{
NSString *amendedInput = [NSString stringWithFormat:@"<input type=\"text\" name=\"vinnumber\" value=\"%i\">", vinNumber];
NSString *amendedHTML = [html stringByReplacingOccurrencesOfString:@"<input type=\"text\" name=\"vinnumber\">" withString:amendedInput];
[_webview loadHTMLString:amendedHTML baseURL:[NSURL URLWithString:@"http://tk14.webng.com/bswbhtml.html"]];
}
}
}];
结果页面在我的 iPhone 4S 上看起来像这样:
关于ios - 将数据从 iOS 标签字段发送到网站文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18185480/