5

My UIWebView loads a contenteditable html content. I want to let the UIWebView get focused and show keyboard in the UIWebView.

I use [self.webview becomeFirstResponder], but it doesn't work. Is there any work around method?

Thanks.

tangqiaoboy
  • 1,476
  • 1
  • 15
  • 32

4 Answers4

8

This is now possible in iOS 6. See the boolean keyboardDisplayRequiresUserAction in UIWebView. Once set to NO then you can use focus() to set the cursor.

eliajf
  • 485
  • 7
  • 15
  • This puts it in focus, but doesnt actually bring up the keyboard. – scord Feb 07 '17 at 18:42
  • Setting this to NO makes it possible to set the cursor and bring up the cursor in Javascript. You can set .focus() on the element and the keyboard will appear. No such similar option exists for WkWebView however. – eliajf Feb 07 '17 at 23:09
2

In iOS 6 > you need to 1) set your webview to keyboardDisplayRequiresUserAction = NO, and 2) call focus. Here's the code:

self.webView.keyboardDisplayRequiresUserAction = NO; // put in viewDidLoad

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
        [self.webView
         stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].focus()"];
    return YES;
}

You can use document.getElementById('someId').focus() as an alternative. I've set the textField as the delegate and used the delegate method textFieldShouldReturn which fires when the return key is pressed.

SmileBot
  • 19,393
  • 7
  • 65
  • 62
1

need set "Visible at Launch" checkbox in you window object - keyboard will show automaticaly on input field

Svyatoslavik
  • 166
  • 1
  • 1
  • 7
1

UIwebview is not editable and you can not make it first responder. Where as you can use javascripts for that purpose , try out this answer

UIWebView with contentEditable (html editing), first responder handling?

Community
  • 1
  • 1
Dushyant Singh
  • 721
  • 4
  • 13
  • Thank you, I've already set the html content to editable using javascript. But I can not let the UIWebView get focused. And the link you provide has no answer about become first responser in UIWebView.Can you give me some sample code about this? – tangqiaoboy Mar 23 '12 at 09:03
  • jScriptString = [NSString stringWithFormat:@"var field = document.activeElement;" "field.value='%@';", symbol.data]; [myWebView stringByEvaluatingJavaScriptFromString:jScriptString]; jScriptString = [NSString stringWithFormat:@"document.activeElement.form.submit();"]; [myWebView stringByEvaluatingJavaScriptFromString:jScriptString]; – Dushyant Singh Mar 23 '12 at 10:04