您需要修改网页上输入字段的 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 上生成的页面如下所示:
