The problem is that if the user starts typing in a field and some third party js (like some captcha) starts, then the keyboard will close automatically which is bad user experience.
This is what I was able to figure out.
1. When an iframe starts loading its internal URL, first we call shouldstartloadwithrequest and return YES so that we load that url.
2. At some point between shouldstartloadwithrequest and webviewdidstartload, we call something equivalent to document.activeElement.blur(), which hides the keyboard and unfocus the input the user is typing to.
3. Because of the input gets unfocused, we can catch the UIKeyboardWillHideNotification to call a function.
4. It seems like UIKeyboardWillHideNotification is called before webviewdidstartload and after shouldstartloadwithrequest .
This is what I tried.
1. Save the last input focused by a user in js.
2. Set a flag to true in shouldstartloadwithrequest if the url to be loaded is from an iframe.
3. Check that flag in the function called by UIKeyboardWillHideNotification, and if it is set to true, call a js that focuses again the last input we saved in js.
4. Set the flag to false in webviewdidstartload.
This way the keyboard only flicks in a barely noticeable way, which is fine for now. The problem is that this solution rests on assumptions about the behavior of UIWebView and maybe other code.
As a side note, we are unable to move to WKWebView soon, as our code has a lot of code on top of UIWebView and we have to move each of them individually, which takes a lot of time. But for the time being we don't want our users to have a bad user experience.
So it would be nice if someone could help and answer:
Is there a better way to solve this problem in a short amount of time?
On what conditions is webviewdidstartload called after shouldstartloadwithrequest?
Is the flow of shouldstartloadwithrequest => KeyboardWillHide => document.activeElement.blur() => webviewdidstartload called in this order consistently or is there some asynchronous behavior that I'm unable to reproduce?