在phonegap中打开软键盘时,showkeyboard / hidekeyboard事件不会在ios 7上触发

时间:2014-01-21 11:52:26

标签: android ios jquery-mobile cordova

我有一个非常简单的代码,只是为了在软键盘打开时尝试发出警报。我只是想确定这是一种显示键盘打开的可行方法。

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

这些事件永远不会在IOS上触发。只有android。我确实曾经看到有一个插件在IO7上有帮助,但我现在根本找不到它。

我正在使用带有 PG Build 的PG 3.1.0。

编辑:我只想强调这是针对 Phonegap Build ,这意味着据我所知,我不会使用任何自定义插件。只列出此处列出的内容:https://build.phonegap.com/plugins

1 个答案:

答案 0 :(得分:3)

我也遇到了同样的问题。没有可用的插件。最后,我为iOS添加了新的插件方法。

在您的应用程序上添加CDVNotification插件,并添加以下方法和属性。

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

您可以在打开时获取回调并使用以下代码隐藏键盘。

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

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