简体   繁体   中英

showkeyboard/hidekeyboard events not firing on ios 7 when opening the Soft Keyboard in phonegap

I have a very simple bit of code just to try and throw up an alert when the soft keyboard is open. I'm just trying to establish that this a viable method for showing the keyboard is open.

document.addEventListener('deviceready', function () {
    $.app.deviceReady();

    document.addEventListener("showkeyboard", function(){ alert("Keyboard is ON");}, false);
    document.addEventListener("hidekeyboard", function(){ alert("Keyboard is OFF");}, false);
}, false);

These events never get triggered on IOS. Only android. I did see at one point there was a plugin that helps with this on IO7 but I can't find it at all now.

I'm using PG 3.1.0 with PG Build .

Edit: I just want to highlight that this is for Phonegap Build , that means as far as I know, I don't get to use any custom plugins. Only the ones listed here: https://build.phonegap.com/plugins

I also faced the same problems. There is no plugins available for this. Finally i have added new plugin methods for iOS.

Add the CDVNotification plugin on you application and add the following methods and properties.

CDVNotification.h

@property (strong) NSString* keyboardShowcallbackId;
@property (strong) NSString* keyboardHidecallbackId;

- (void)keyboardShow:(CDVInvokedUrlCommand*)command;
- (void)keyboardHide:(CDVInvokedUrlCommand*)command;

CDVNotification.m

//Keyboard notifications.
- (void)keyboardShow:(CDVInvokedUrlCommand*)command {

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];

self.keyboardShowcallbackId = command.callbackId;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShowCallback:)
                                             name:UIKeyboardWillShowNotification object:nil];
}
- (void)keyboardHide:(CDVInvokedUrlCommand*)command {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

self.keyboardHidecallbackId = command.callbackId;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardHideCallback:)
                                             name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyBoardHideCallback:(NSNotification*)notification {
     if (self.keyboardHidecallbackId) {
       CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
    [result setKeepCallbackAsBool:YES];

       [self.commandDelegate sendPluginResult:result callbackId:self.keyboardHidecallbackId];
     }
  }

- (void)keyBoardShowCallback:(NSNotification*)notification  {
     if (self.keyboardShowcallbackId) {
        CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];

        [result setKeepCallbackAsBool:YES];
        [self.commandDelegate sendPluginResult:result callbackId:self.keyboardShowcallbackId];
     }
}

You can get the callback while open and hide the keyboards using following codes.

 cordova.exec(function(){alert("Keyboard is ON");},function(){alert("error");},"Notification","keyboardShow",[]);

 cordova.exec(function(){alert("keyboard is OFF");},function(){alert("error");},"Notification","keyboardHide",[]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM