Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS - How to avoid keyboard from hiding when uiwebview reloads

I have a webview that loads a web chat client.

As every chat, the page has a textfield to input text.

The problem is that when the user opens the keyboard it is automatically hidden after a short time due to several ajax requests that are reloading the page. This becomes really annoying for the user as he or she can't input a complete sentence before the keyboard hides.

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

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

I tried to "hang" the request with a Thread sleep in the same method, but it happens in the Main Thread so it freezes the entire application.

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

like image 978
htafoya Avatar asked Nov 16 '12 03:11

htafoya


1 Answers

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

First use DOM to check if the webView firstResonder

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

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

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if([self isWebViewFirstResponder] &&
   navigationType != UIWebViewNavigationTypeFormSubmitted) {
        return NO;
    } else {
        return YES;
    }
}
like image 192
Basheer_CAD Avatar answered Oct 02 '22 06:10

Basheer_CAD