commonroutesServer/www/plugins/ionic-plugin-keyboard/src/ios/IonicKeyboard.m
www/plugins/ionic-plugin-keyboard/src/ios/IonicKeyboard.mRaw
1#import "IonicKeyboard.h"
2// #import "UIWebViewExtension.h"
3#import <Cordova/CDVAvailability.h>
4
5@implementation IonicKeyboard
6
7@synthesize hideKeyboardAccessoryBar = _hideKeyboardAccessoryBar;
8@synthesize disableScroll = _disableScroll;
9//@synthesize styleDark = _styleDark;
10
11- (void)pluginInitialize {
12
13 Class wkClass = NSClassFromString([@[@"UI", @"Web", @"Browser", @"View"] componentsJoinedByString:@""]);
14 wkMethod = class_getInstanceMethod(wkClass, @selector(inputAccessoryView));
15 wkOriginalImp = method_getImplementation(wkMethod);
16 Class uiClass = NSClassFromString([@[@"WK", @"Content", @"View"] componentsJoinedByString:@""]);
17 uiMethod = class_getInstanceMethod(uiClass, @selector(inputAccessoryView));
18 uiOriginalImp = method_getImplementation(uiMethod);
19 nilImp = imp_implementationWithBlock(^(id _s) {
20 return nil;
21 });
22
23 //set defaults
24 self.hideKeyboardAccessoryBar = YES;
25 self.disableScroll = NO;
26 //self.styleDark = NO;
27
28 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
29 __weak IonicKeyboard* weakSelf = self;
30 _keyboardShowObserver = [nc addObserverForName:UIKeyboardWillShowNotification
31 object:nil
32 queue:[NSOperationQueue mainQueue]
33 usingBlock:^(NSNotification* notification) {
34
35 CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
36 keyboardFrame = [self.viewController.view convertRect:keyboardFrame fromView:nil];
37
38 [weakSelf.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.plugins.Keyboard.isVisible = true; cordova.fireWindowEvent('native.keyboardshow', { 'keyboardHeight': %@ }); ", [@(keyboardFrame.size.height) stringValue]]];
39
40 //deprecated
41 [weakSelf.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireWindowEvent('native.showkeyboard', { 'keyboardHeight': %@ }); ", [@(keyboardFrame.size.height) stringValue]]];
42 }];
43
44 _keyboardHideObserver = [nc addObserverForName:UIKeyboardWillHideNotification
45 object:nil
46 queue:[NSOperationQueue mainQueue]
47 usingBlock:^(NSNotification* notification) {
48 [weakSelf.commandDelegate evalJs:@"cordova.plugins.Keyboard.isVisible = false; cordova.fireWindowEvent('native.keyboardhide'); "];
49
50 //deprecated
51 [weakSelf.commandDelegate evalJs:@"cordova.fireWindowEvent('native.hidekeyboard'); "];
52 }];
53}
54
55- (BOOL)disableScroll {
56 return _disableScroll;
57}
58
59- (void)setDisableScroll:(BOOL)disableScroll {
60 if (disableScroll == _disableScroll) {
61 return;
62 }
63 if (disableScroll) {
64 self.webView.scrollView.scrollEnabled = NO;
65 self.webView.scrollView.delegate = self;
66 }
67 else {
68 self.webView.scrollView.scrollEnabled = YES;
69 self.webView.scrollView.delegate = nil;
70 }
71
72 _disableScroll = disableScroll;
73}
74
75//keyboard swizzling inspired by:
76//https://github.com/cjpearson/cordova-plugin-keyboard/
77
78- (BOOL)hideKeyboardAccessoryBar {
79 return _hideKeyboardAccessoryBar;
80}
81
82- (void)setHideKeyboardAccessoryBar:(BOOL)hideKeyboardAccessoryBar {
83 if (hideKeyboardAccessoryBar == _hideKeyboardAccessoryBar) {
84 return;
85 }
86
87 if (hideKeyboardAccessoryBar) {
88 method_setImplementation(wkMethod, nilImp);
89 method_setImplementation(uiMethod, nilImp);
90 } else {
91 method_setImplementation(wkMethod, wkOriginalImp);
92 method_setImplementation(uiMethod, uiOriginalImp);
93 }
94
95 _hideKeyboardAccessoryBar = hideKeyboardAccessoryBar;
96}
97
98/*
99- (BOOL)styleDark {
100 return _styleDark;
101}
102
103- (void)setStyleDark:(BOOL)styleDark {
104 if (styleDark == _styleDark) {
105 return;
106 }
107 if (styleDark) {
108 self.webView.styleDark = YES;
109 }
110 else {
111 self.webView.styleDark = NO;
112 }
113
114 _styleDark = styleDark;
115}
116*/
117
118
119/* ------------------------------------------------------------- */
120
121- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
122 [scrollView setContentOffset: CGPointZero];
123}
124
125/* ------------------------------------------------------------- */
126
127- (void)dealloc {
128 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
129
130 [nc removeObserver:self name:UIKeyboardWillShowNotification object:nil];
131 [nc removeObserver:self name:UIKeyboardWillHideNotification object:nil];
132}
133
134/* ------------------------------------------------------------- */
135
136- (void) disableScroll:(CDVInvokedUrlCommand*)command {
137 if (!command.arguments || ![command.arguments count]){
138 return;
139 }
140 id value = [command.arguments objectAtIndex:0];
141 if (value != [NSNull null]) {
142 self.disableScroll = [value boolValue];
143 }
144}
145
146- (void) hideKeyboardAccessoryBar:(CDVInvokedUrlCommand*)command {
147 if (!command.arguments || ![command.arguments count]){
148 return;
149 }
150 id value = [command.arguments objectAtIndex:0];
151 if (value != [NSNull null]) {
152 self.hideKeyboardAccessoryBar = [value boolValue];
153 }
154}
155
156- (void) close:(CDVInvokedUrlCommand*)command {
157 [self.webView endEditing:YES];
158}
159
160- (void) show:(CDVInvokedUrlCommand*)command {
161 NSLog(@"Showing keyboard not supported in iOS due to platform limitations.");
162}
163
164/*
165- (void) styleDark:(CDVInvokedUrlCommand*)command {
166 if (!command.arguments || ![command.arguments count]){
167 return;
168 }
169 id value = [command.arguments objectAtIndex:0];
170
171 self.styleDark = [value boolValue];
172}
173*/
174
175@end
176