2

I'm opening a web page on UIWebview and this page has a textfield and a virtual keyboard the to enter password. But when edit the textfield iPhone virtual keyboard appears as well. I wanted to dismiss iPhone keyboard and on the same time continue editing the textfield. I have tried the following but it did not work.

[webView stringByEvaluatingJavaScriptFromString:@"document.activeElement.blur()"];

And also tried to register for Keyboard notification so I can dimiss the keyboard but did not succeed. I would appreciate any ideas about how to achieve this. Thanks in advance.

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector (keyboardDidShow:)
                                                 name: UIKeyboardDidShowNotification object:nil];



-(void) keyboardDidShow: (NSNotification *)notif 
{
// I want to dismiss the keyboard 
}
Sarah
  • 1,595
  • 3
  • 25
  • 34

4 Answers4

7

try this

[webView endEditing:YES]

You also can try this link if above solution does not work - Hide keyboard in UIwebView

Community
  • 1
  • 1
iOSPawan
  • 2,884
  • 2
  • 25
  • 50
2

Add method that go over all the webview subviews and resign the first responder

     -(BOOL)resignFirstResponderAction:(UIView *)view{
        if (view.isFirstResponder){
            [view resignFirstResponder];
            return YES;
        }
        
        for (UIView *subView in view.subviews) {
            if ([self resignFirstResponderAction:subView]){
                return YES;
            }
        }
        return NO;
     }

then just call this method with the webview:

     [self resignFirstResponderAction:webView];

Edit

it seems there is much simpler solution:

[webView endEditing:YES]

I leave the original answer as it might help in some cases

poleB
  • 71
  • 1
  • 3
0

You can use javascript to take the focus away from the html element when it gets clicked like this:

 <input type="text" onClick="document.getElementById('myInput').blur();" id="myInput"/>
Alex Stanciu
  • 5,183
  • 2
  • 24
  • 31
0

You need to add next category of UIView to your code
And the method above to your implementation Add the next code to your .m file, before the @implementation

@implementation UIView (FindFirstResponder)

- (UIView *)findFirstResponder
{
    if (self.isFirstResponder) {        
        return self;     
    }

    for (UIView *subView in self.subviews) {
        UIView *firstResponder = [subView findFirstResponder];

        if (firstResponder != nil) {
            return firstResponder;
        }
    }

    return nil;
}
@end


-(void)hideWebViewFirstResponser{
    UIView *firstResponderOwner=[webView findFirstResponder];
    if(firstResponderOwner) {
        [firstResponderOwner resignFirstResponder];
    }
}