IonicKeyboard.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 = YES;
  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. //
  55. // - (void)setHideKeyboardAccessoryBar:(BOOL)hideKeyboardAccessoryBar {
  56. // if (hideKeyboardAccessoryBar == _hideKeyboardAccessoryBar || ![self.webView isKindOfClass:[UIWebView class]]) {
  57. // return;
  58. // }
  59. // if (hideKeyboardAccessoryBar) {
  60. // ((UIWebView*)self.webView).hackishlyHidesInputAccessoryView = YES;
  61. // }
  62. // else {
  63. // ((UIWebView*)self.webView).hackishlyHidesInputAccessoryView = NO;
  64. // }
  65. //
  66. // _hideKeyboardAccessoryBar = hideKeyboardAccessoryBar;
  67. // }
  68. /*
  69. - (BOOL)styleDark {
  70. return _styleDark;
  71. }
  72. - (void)setStyleDark:(BOOL)styleDark {
  73. if (styleDark == _styleDark) {
  74. return;
  75. }
  76. if (styleDark) {
  77. self.webView.styleDark = YES;
  78. }
  79. else {
  80. self.webView.styleDark = NO;
  81. }
  82. _styleDark = styleDark;
  83. }
  84. */
  85. /* ------------------------------------------------------------- */
  86. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  87. [scrollView setContentOffset: CGPointZero];
  88. }
  89. /* ------------------------------------------------------------- */
  90. - (void)dealloc {
  91. NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
  92. [nc removeObserver:self name:UIKeyboardWillShowNotification object:nil];
  93. [nc removeObserver:self name:UIKeyboardWillHideNotification object:nil];
  94. }
  95. /* ------------------------------------------------------------- */
  96. - (void) disableScroll:(CDVInvokedUrlCommand*)command {
  97. if (!command.arguments || ![command.arguments count]){
  98. return;
  99. }
  100. id value = [command.arguments objectAtIndex:0];
  101. if (value != [NSNull null]) {
  102. self.disableScroll = [value boolValue];
  103. }
  104. }
  105. // - (void) hideKeyboardAccessoryBar:(CDVInvokedUrlCommand*)command {
  106. // if (!command.arguments || ![command.arguments count]){
  107. // return;
  108. // }
  109. // id value = [command.arguments objectAtIndex:0];
  110. // if (value != [NSNull null]) {
  111. // self.hideKeyboardAccessoryBar = [value boolValue];
  112. // }
  113. // }
  114. - (void) close:(CDVInvokedUrlCommand*)command {
  115. [self.webView endEditing:YES];
  116. }
  117. - (void) show:(CDVInvokedUrlCommand*)command {
  118. NSLog(@"Showing keyboard not supported in iOS due to platform limitations.");
  119. }
  120. /*
  121. - (void) styleDark:(CDVInvokedUrlCommand*)command {
  122. if (!command.arguments || ![command.arguments count]){
  123. return;
  124. }
  125. id value = [command.arguments objectAtIndex:0];
  126. self.styleDark = [value boolValue];
  127. }
  128. */
  129. @end