Skip to content
This repository was archived by the owner on Apr 2, 2018. It is now read-only.

Commit d2ff48e

Browse files
tony--tlancina
authored andcommitted
Swizzle it just a little bit (#197)
* initial implementation with swizzle tracking * remove swizzle tracking, add class prefix checking * reduce unnecessary string allocation in unswizzle * revert README.md to reenable cordova.plugins.Keyboard.hideKeyboardAccessoryBar * added attributions for swizzling approach * fixed indenting (tabs->spaces) * fixed indenting (tabs->spaces) * change approach from class swizzling to method swizzling * replaced c function to IMP cast with block-based imp * removed cruft
1 parent a0f44f8 commit d2ff48e

File tree

7 files changed

+53
-157
lines changed

7 files changed

+53
-157
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The `cordova.plugins.Keyboard` object provides functions to make interacting wit
88
Methods
99
-------
1010

11-
- ~~cordova.plugins.Keyboard.hideKeyboardAccessoryBar~~ (**removed in 2.0, see below**)
11+
- cordova.plugins.Keyboard.hideKeyboardAccessoryBar
1212
- cordova.plugins.Keyboard.close
1313
- cordova.plugins.Keyboard.disableScroll
1414
- cordova.plugins.Keyboard.show
@@ -30,11 +30,9 @@ These events are fired on the window.
3030

3131
# API reference
3232

33-
~~Keyboard.hideKeyboardAccessoryBar~~
33+
Keyboard.hideKeyboardAccessoryBar
3434
=================
3535

36-
**NOTE: This method started causing apps to be rejected from the App Store, so has been removed until a workaround is found.**
37-
3836
Hide the keyboard accessory bar with the next, previous and done buttons.
3937

4038
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

plugin.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,8 @@
3838
<param name="ios-package" value="IonicKeyboard" onload="true" />
3939
</feature>
4040
</config-file>
41-
4241
<header-file src="src/ios/IonicKeyboard.h" />
4342
<source-file src="src/ios/IonicKeyboard.m" />
44-
<!--
45-
<header-file src="src/ios/UIWebViewExtension.h" />
46-
<source-file src="src/ios/UIWebViewExtension.m" />
47-
-->
4843
</platform>
4944

5045
<!-- browser -->

src/ios/IonicKeyboard.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#import <Cordova/CDVPlugin.h>
2+
#import <objc/runtime.h>
23

34
@interface IonicKeyboard : CDVPlugin <UIScrollViewDelegate> {
45
@protected
56
id _keyboardShowObserver, _keyboardHideObserver;
7+
IMP wkOriginalImp, uiOriginalImp, nilImp;
8+
Method wkMethod, uiMethod;
69
}
710

8-
// @property (readwrite, assign) BOOL hideKeyboardAccessoryBar;
11+
@property (readwrite, assign) BOOL hideKeyboardAccessoryBar;
912
@property (readwrite, assign) BOOL disableScroll;
1013
//@property (readwrite, assign) BOOL styleDark;
1114

src/ios/IonicKeyboard.m

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,29 @@
44

55
@implementation IonicKeyboard
66

7-
// @synthesize hideKeyboardAccessoryBar = _hideKeyboardAccessoryBar;
7+
@synthesize hideKeyboardAccessoryBar = _hideKeyboardAccessoryBar;
88
@synthesize disableScroll = _disableScroll;
99
//@synthesize styleDark = _styleDark;
1010

1111
- (void)pluginInitialize {
1212

13-
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
14-
__weak IonicKeyboard* weakSelf = self;
15-
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+
1623
//set defaults
17-
// self.hideKeyboardAccessoryBar = YES;
24+
self.hideKeyboardAccessoryBar = YES;
1825
self.disableScroll = NO;
1926
//self.styleDark = NO;
20-
27+
28+
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
29+
__weak IonicKeyboard* weakSelf = self;
2130
_keyboardShowObserver = [nc addObserverForName:UIKeyboardWillShowNotification
2231
object:nil
2332
queue:[NSOperationQueue mainQueue]
@@ -42,6 +51,7 @@ - (void)pluginInitialize {
4251
[weakSelf.commandDelegate evalJs:@"cordova.fireWindowEvent('native.hidekeyboard'); "];
4352
}];
4453
}
54+
4555
- (BOOL)disableScroll {
4656
return _disableScroll;
4757
}
@@ -62,24 +72,28 @@ - (void)setDisableScroll:(BOOL)disableScroll {
6272
_disableScroll = disableScroll;
6373
}
6474

75+
//keyboard swizzling inspired by:
76+
//https://github.com/cjpearson/cordova-plugin-keyboard/
77+
78+
- (BOOL)hideKeyboardAccessoryBar {
79+
return _hideKeyboardAccessoryBar;
80+
}
6581

66-
// - (BOOL)hideKeyboardAccessoryBar {
67-
// return _hideKeyboardAccessoryBar;
68-
// }
69-
//
70-
// - (void)setHideKeyboardAccessoryBar:(BOOL)hideKeyboardAccessoryBar {
71-
// if (hideKeyboardAccessoryBar == _hideKeyboardAccessoryBar || ![self.webView isKindOfClass:[UIWebView class]]) {
72-
// return;
73-
// }
74-
// if (hideKeyboardAccessoryBar) {
75-
// ((UIWebView*)self.webView).hackishlyHidesInputAccessoryView = YES;
76-
// }
77-
// else {
78-
// ((UIWebView*)self.webView).hackishlyHidesInputAccessoryView = NO;
79-
// }
80-
//
81-
// _hideKeyboardAccessoryBar = hideKeyboardAccessoryBar;
82-
// }
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+
}
8397

8498
/*
8599
- (BOOL)styleDark {
@@ -129,15 +143,15 @@ - (void) disableScroll:(CDVInvokedUrlCommand*)command {
129143
}
130144
}
131145

132-
// - (void) hideKeyboardAccessoryBar:(CDVInvokedUrlCommand*)command {
133-
// if (!command.arguments || ![command.arguments count]){
134-
// return;
135-
// }
136-
// id value = [command.arguments objectAtIndex:0];
137-
// if (value != [NSNull null]) {
138-
// self.hideKeyboardAccessoryBar = [value boolValue];
139-
// }
140-
// }
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+
}
141155

142156
- (void) close:(CDVInvokedUrlCommand*)command {
143157
[self.webView endEditing:YES];

src/ios/UIWebViewExtension.h

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/ios/UIWebViewExtension.m

Lines changed: 0 additions & 109 deletions
This file was deleted.

www/ios/keyboard.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ var Keyboard = function() {
88
};
99

1010
Keyboard.hideKeyboardAccessoryBar = function(hide) {
11-
// exec(null, null, "Keyboard", "hideKeyboardAccessoryBar", [hide]);
12-
console.warn('hideKeyboardAccessoryBar has been temporarily removed on iOS until an Apple-approved method is found.');
11+
exec(null, null, "Keyboard", "hideKeyboardAccessoryBar", [hide]);
1312
};
1413

1514
Keyboard.close = function() {

0 commit comments

Comments
 (0)