Monday, June 3, 2013

UIWebView keyboardDisplayRequiresUserAction example in Objective C (iOS).

UIWebView keyboardDisplayRequiresUserAction

A Boolean value indicating whether web content can programmatically display the keyboard.

@property (nonatomic) BOOL keyboardDisplayRequiresUserAction

Discussion of [UIWebView keyboardDisplayRequiresUserAction]
When this property is set to YES, the user must explicitly tap the elements in the web view to display the keyboard (or other relevant input view) for that element. When set to NO, a focus event on an element causes the input view to be displayed and associated with that element automatically.

The default value for this property is YES.

UIWebView keyboardDisplayRequiresUserAction example.
Prior to iOS 6 this wasn't possible, but thankfully they've changed that. You can't do it in general for a website, but if you are embedding a UIWebView in your application you can do it now:

UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
webView.keyboardDisplayRequiresUserAction = NO;

Example of [UIWebView keyboardDisplayRequiresUserAction].
Prior to iOS 6, Apple only allowed the keyboard to be brought up following a user interaction. In iOS 6 they've introduced the following property for UIWebView which you merely need to set to NO.

    "yourWebView".keyboardDisplayRequiresUserAction = NO;
Apple sets this by default to "Yes". Now you can call focus() in your JS and the keyboard will appear.

End of UIWebView keyboardDisplayRequiresUserAction example article.