Search code examples
iosobjective-ciphoneuiwebviewbarcode

UIWebview set text on current textfield


Is there a way to set the text on the current textfield on a UIWebview?

I don't need to retrieve the text. I don't need to determine what text field currently has focus.

Basically the user will scan a barcode (with the linea-pro) and if there is a textfield selected in the uiwebview I just want to set the text to the retrieved barcode


Solution

  • You can exec JavaScript function that can set value to text field. Something like this

    NSString *text = @"Text field text";
    NSString *javaScript = [NSString stringWithFormat:@"var textField = document.activeElement;"
                                                       "textField.value = '%@';"
                                                       "document.activeElement.form.submit();", text];
    [webView stringByEvaluatingJavaScriptFromString:javaScript];