IOS - How to avoid hiding the keyboard when restarting uiwebview

I have a webview that a web chat client downloads.

Like every chat, the page has a text box for entering text.

The problem is that when the user opens the keyboard, it automatically hides after a short time due to several ajax requests that reload the page. This becomes very annoying for the user, since he or she cannot enter the full sentence before the keyboard is hidden.

I don’t know why this only happens in iPhone 4S and iPhone 5. In iPhone 4, 3GS and Simulator everything works fine.

I tried to use shouldStartLoadWithRequest to catch the request and load it after the user hides the keyboard, but this disrupts the chat session.

I tried to “hang” the request with the Thread thread in the same method, but this happens in the main thread so that it hangs on the entire application.

Is there a way that I can simply avoid hiding the keyboard?

+7
source share
3 answers

So, after a long research, I found a way to do this, this is not the best, but it helped me a lot.

First use the DOM to check if webView is firstResonder

 - (BOOL)isWebViewFirstResponder { NSString *str = [self.webView stringByEvaluatingJavaScriptFromString:@"document.activeElement.tagName"]; if([[str lowercaseString]isEqualToString:@"input"]) { return YES; } return NO; } 

Then respond to the UIWebViewDelegate method shouldStartLoadWithRequest and return NO if the UIWebView is the first responder

 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { if([self isWebViewFirstResponder] && navigationType != UIWebViewNavigationTypeFormSubmitted) { return NO; } else { return YES; } } 
+7
source

You can use the Notification Center inside your DidLoad method to listen when the keyboard is hiding as follows:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil]; - (void) keyboardWillHide{ [webView becomeFirstResponder]; } 

which will make the web browsing first answer and display the keyboard again. I have not tried this myself, so I hope this works.

-one
source

if your text box is in a UIView

You can use the delegate submission method on the Internet.

 -(void)webViewDidFinishLoad:(UIWebView *)webView { [textField becomeFirstResponder]; } -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { [textField becomeFirstResponder]; } 

otherwise, if textField is included in the UIWebView, then replace textField with webView, as shown below.

 -(void)webViewDidFinishLoad:(UIWebView *)webView { [webView becomeFirstResponder]; } -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { [webView becomeFirstResponder]; } 
-one
source

All Articles