IonicKeyboard.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #import "IonicKeyboard.h"
  2. #import "UIWebViewExtension.h"
  3. #import <Cordova/CDVAvailability.h>
  4. @implementation IonicKeyboard
  5. @synthesize hideKeyboardAccessoryBar = _hideKeyboardAccessoryBar;
  6. @synthesize disableScroll = _disableScroll;
  7. //@synthesize styleDark = _styleDark;
  8. - (void)pluginInitialize {
  9. NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
  10. __weak IonicKeyboard* weakSelf = self;
  11. //set defaults
  12. self.hideKeyboardAccessoryBar = NO;
  13. self.disableScroll = NO;
  14. //self.styleDark = NO;
  15. _keyboardShowObserver = [nc addObserverForName:UIKeyboardWillShowNotification
  16. object:nil
  17. queue:[NSOperationQueue mainQueue]
  18. usingBlock:^(NSNotification* notification) {
  19. CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  20. keyboardFrame = [self.viewController.view convertRect:keyboardFrame fromView:nil];
  21. [weakSelf.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.plugins.Keyboard.isVisible = true; cordova.fireWindowEvent('native.keyboardshow', { 'keyboardHeight': %@ }); ", [@(keyboardFrame.size.height) stringValue]]];
  22. //deprecated
  23. [weakSelf.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireWindowEvent('native.showkeyboard', { 'keyboardHeight': %@ }); ", [@(keyboardFrame.size.height) stringValue]]];
  24. }];
  25. _keyboardHideObserver = [nc addObserverForName:UIKeyboardWillHideNotification
  26. object:nil
  27. queue:[NSOperationQueue mainQueue]
  28. usingBlock:^(NSNotification* notification) {
  29. [weakSelf.commandDelegate evalJs:@"cordova.plugins.Keyboard.isVisible = false; cordova.fireWindowEvent('native.keyboardhide'); "];
  30. //deprecated
  31. [weakSelf.commandDelegate evalJs:@"cordova.fireWindowEvent('native.hidekeyboard'); "];
  32. }];
  33. }
  34. - (BOOL)disableScroll {
  35. return _disableScroll;
  36. }
  37. - (void)setDisableScroll:(BOOL)disableScroll {
  38. if (disableScroll == _disableScroll) {
  39. return;
  40. }
  41. if (disableScroll) {
  42. self.webView.scrollView.scrollEnabled = NO;
  43. self.webView.scrollView.delegate = self;
  44. }
  45. else {
  46. self.webView.scrollView.scrollEnabled = YES;
  47. self.webView.scrollView.delegate = nil;
  48. }
  49. _disableScroll = disableScroll;
  50. }
  51. - (BOOL)hideKeyboardAccessoryBar {
  52. return _hideKeyboardAccessoryBar;
  53. }
  54. - (void)setHideKeyboardAccessoryBar:(BOOL)hideKeyboardAccessoryBar {
  55. if (hideKeyboardAccessoryBar == _hideKeyboardAccessoryBar) {
  56. return;
  57. }
  58. if (hideKeyboardAccessoryBar) {
  59. self.webView.hackishlyHidesInputAccessoryView = YES;
  60. }
  61. else {
  62. self.webView.hackishlyHidesInputAccessoryView = NO;
  63. }
  64. _hideKeyboardAccessoryBar = hideKeyboardAccessoryBar;
  65. }
  66. /*
  67. - (BOOL)styleDark {
  68. return _styleDark;
  69. }
  70. - (void)setStyleDark:(BOOL)styleDark {
  71. if (styleDark == _styleDark) {
  72. return;
  73. }
  74. if (styleDark) {
  75. self.webView.styleDark = YES;
  76. }
  77. else {
  78. self.webView.styleDark = NO;
  79. }
  80. _styleDark = styleDark;
  81. }
  82. */
  83. /* ------------------------------------------------------------- */
  84. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  85. [scrollView setContentOffset: CGPointZero];
  86. }
  87. /* ------------------------------------------------------------- */
  88. - (void)dealloc {
  89. NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
  90. [nc removeObserver:self name:UIKeyboardWillShowNotification object:nil];
  91. [nc removeObserver:self name:UIKeyboardWillHideNotification object:nil];
  92. }
  93. /* ------------------------------------------------------------- */
  94. - (void) disableScroll:(CDVInvokedUrlCommand*)command {
  95. if (!command.arguments || ![command.arguments count]){
  96. return;
  97. }
  98. id value = [command.arguments objectAtIndex:0];
  99. self.disableScroll = [value boolValue];
  100. }
  101. - (void) hideKeyboardAccessoryBar:(CDVInvokedUrlCommand*)command {
  102. if (!command.arguments || ![command.arguments count]){
  103. return;
  104. }
  105. id value = [command.arguments objectAtIndex:0];
  106. self.hideKeyboardAccessoryBar = [value boolValue];
  107. }
  108. - (void) close:(CDVInvokedUrlCommand*)command {
  109. [self.webView endEditing:YES];
  110. }
  111. - (void) show:(CDVInvokedUrlCommand*)command {
  112. NSLog(@"Showing keyboard not supported in iOS due to platform limitations.");
  113. }
  114. /*
  115. - (void) styleDark:(CDVInvokedUrlCommand*)command {
  116. if (!command.arguments || ![command.arguments count]){
  117. return;
  118. }
  119. id value = [command.arguments objectAtIndex:0];
  120. self.styleDark = [value boolValue];
  121. }
  122. */
  123. @end