Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show keyboard in contenteditable UIWebView programmatically

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.

like image 327
tangqiaoboy Avatar asked Mar 23 '12 08:03

tangqiaoboy


4 Answers

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.

like image 150
eliajf Avatar answered Nov 14 '22 23:11

eliajf


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.

like image 33
smileBot Avatar answered Nov 14 '22 22:11

smileBot


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?

like image 1
Dushyant Singh Avatar answered Nov 14 '22 23:11

Dushyant Singh


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

like image 1
Svyatoslavik Avatar answered Nov 14 '22 22:11

Svyatoslavik