0

I have an iOS web app with a WKWebView. I would like an HTML input to auto focus when the app is opened. What I've tried and hasn't worked:

  1. html autofocus tag

    <input autofocus />
    
  2. JavaScript

    document.getElementById("my_input").focus();
    

Thank you for your help.

Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
quemeful
  • 9,542
  • 4
  • 60
  • 69
  • Have you tried http://stackoverflow.com/questions/5527639/how-can-i-focus-on-an-input-field-when-a-phonegap-page-loads ?? – Cordovaing Feb 20 '16 at 13:15
  • I'm sure that would work if I was making a PhoneGap application, but I completely wrote my own web app. – quemeful Feb 20 '16 at 13:42

1 Answers1

1

It is not possible in iOS due to security issues. The input can only be focused on a tap event.

quemeful
  • 9,542
  • 4
  • 60
  • 69
  • Are you sure about this? I am working on an iOS app with wkWebView and we have it so that every page that loads autofocuses on a specific text field. Is this against Apple's terms for apps uploaded to the app store? – Vemonus Aug 18 '16 at 20:29
  • How do you do it? I've tried to do this for so long and found nothing – quemeful Aug 18 '16 at 20:31
  • We are using the JavaScript to handle it. The wkWebView is not much more than a framework for our JavaScript. – Vemonus Aug 18 '16 at 20:37
  • I see. That is what we do with our apps too (the WKWebView framework part) What JavaScript is used to autofocus, if you don't mind we asking. – quemeful Aug 18 '16 at 20:38
  • I've tried this (document.getElementById("my_input").focus();) but to no avail. – quemeful Aug 18 '16 at 20:38
  • I believe I misspoke; it's not handled by the JavaScript. The text fields that we are wanting to be autofocused are just instantiated with an autofocus attribute Reference: http://www.w3schools.com/tags/att_input_autofocus.asp – Vemonus Aug 18 '16 at 20:41