Reputation: 353
Javascript n00b here, this question has been asked before but no straight forward answer was given.
I am trying to launch the keyboard for an editable UIWebView when the view loads.
So far I have the following code
in index.html:
<html>
<head>
<script Language="text/javascript">
function showKeyBoard(){
document.forms['myform'].elements['mytextfield'].focus();
}
</script>
</head>
<body OnLoad="document.myform.mytextfield.focus();">
<form name="myform">
<textarea type="text" name="mytextfield" cols="40">Tap here to enter text</textarea>
</form>
</body>
</html>
In my view controller I have:
-(void)viewDidLoad
{
[super viewDidLoad];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];
}
The same view controller is also the webView's delegate with the following code:
-(void)webViewDidFinishLoad:(UIWebView *)aWebView
{
[aWebView stringByEvaluatingJavaScriptFromString:@"showKeyBoard()"];
}
Is there anything wrong with my java script or iOS?
The textarea is not necessary for the app I only included it to have a focus-able form element.
I am open to any solution that will get the UIWebView keyboard to launch when the view loads. I have been trying to work out a solution for weeks and I've gotten no where.
Edit: The focus works when I open index.html with a web browser.
Upvotes: 2
Views: 1546
Reputation: 456
Starting in iOS 6, you can set [aWebView setKeyboardDisplayRequiresUserAction:NO].
Upvotes: 5