htafoya
htafoya

Reputation: 19273

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?

Upvotes: 7

Views: 3130

Answers (3)

Basheer_CAD
Basheer_CAD

Reputation: 4919

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;
    }
}

Upvotes: 7

Coder_A_D
Coder_A_D

Reputation: 340

if your text field is on UIView

You can use web view Delegate method

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

otherwise if textField is on UIWebView then replace textField with webView as shown below.

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

Upvotes: -1

Orfeas
Orfeas

Reputation: 9

You can use Notification center inside your DidLoad method to listen when the keyboard will hide like this:

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(keyboardWillHide)
                                         name:UIKeyboardWillHideNotification
                                       object:nil];

- (void) keyboardWillHide{

  [webView becomeFirstResponder];
}

which will make the web view first responder and show the keyboard again. I haven't tried it myself so hopefully it will do the trick..

Upvotes: -1

Related Questions