summaryrefslogtreecommitdiff
path: root/plugins/com.ionic.keyboard/src/ios
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/com.ionic.keyboard/src/ios')
-rw-r--r--plugins/com.ionic.keyboard/src/ios/IonicKeyboard.h13
-rw-r--r--plugins/com.ionic.keyboard/src/ios/IonicKeyboard.m160
-rw-r--r--plugins/com.ionic.keyboard/src/ios/UIWebViewExtension.h4
-rw-r--r--plugins/com.ionic.keyboard/src/ios/UIWebViewExtension.m109
4 files changed, 286 insertions, 0 deletions
diff --git a/plugins/com.ionic.keyboard/src/ios/IonicKeyboard.h b/plugins/com.ionic.keyboard/src/ios/IonicKeyboard.h
new file mode 100644
index 00000000..b54f430d
--- /dev/null
+++ b/plugins/com.ionic.keyboard/src/ios/IonicKeyboard.h
@@ -0,0 +1,13 @@
+#import <Cordova/CDVPlugin.h>
+
+@interface IonicKeyboard : CDVPlugin <UIScrollViewDelegate> {
+ @protected
+ id _keyboardShowObserver, _keyboardHideObserver;
+}
+
+@property (readwrite, assign) BOOL hideKeyboardAccessoryBar;
+@property (readwrite, assign) BOOL disableScroll;
+//@property (readwrite, assign) BOOL styleDark;
+
+@end
+
diff --git a/plugins/com.ionic.keyboard/src/ios/IonicKeyboard.m b/plugins/com.ionic.keyboard/src/ios/IonicKeyboard.m
new file mode 100644
index 00000000..045cc65f
--- /dev/null
+++ b/plugins/com.ionic.keyboard/src/ios/IonicKeyboard.m
@@ -0,0 +1,160 @@
+#import "IonicKeyboard.h"
+#import "UIWebViewExtension.h"
+#import <Cordova/CDVAvailability.h>
+
+@implementation IonicKeyboard
+
+@synthesize hideKeyboardAccessoryBar = _hideKeyboardAccessoryBar;
+@synthesize disableScroll = _disableScroll;
+//@synthesize styleDark = _styleDark;
+
+- (void)pluginInitialize {
+
+ NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
+ __weak IonicKeyboard* weakSelf = self;
+
+ //set defaults
+ self.hideKeyboardAccessoryBar = NO;
+ self.disableScroll = NO;
+ //self.styleDark = NO;
+
+ _keyboardShowObserver = [nc addObserverForName:UIKeyboardWillShowNotification
+ object:nil
+ queue:[NSOperationQueue mainQueue]
+ usingBlock:^(NSNotification* notification) {
+
+ CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
+ keyboardFrame = [self.viewController.view convertRect:keyboardFrame fromView:nil];
+
+ [weakSelf.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.plugins.Keyboard.isVisible = true; cordova.fireWindowEvent('native.keyboardshow', { 'keyboardHeight': %@ }); ", [@(keyboardFrame.size.height) stringValue]]];
+
+ //deprecated
+ [weakSelf.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireWindowEvent('native.showkeyboard', { 'keyboardHeight': %@ }); ", [@(keyboardFrame.size.height) stringValue]]];
+ }];
+
+ _keyboardHideObserver = [nc addObserverForName:UIKeyboardWillHideNotification
+ object:nil
+ queue:[NSOperationQueue mainQueue]
+ usingBlock:^(NSNotification* notification) {
+ [weakSelf.commandDelegate evalJs:@"cordova.plugins.Keyboard.isVisible = false; cordova.fireWindowEvent('native.keyboardhide'); "];
+
+ //deprecated
+ [weakSelf.commandDelegate evalJs:@"cordova.fireWindowEvent('native.hidekeyboard'); "];
+ }];
+}
+- (BOOL)disableScroll {
+ return _disableScroll;
+}
+
+- (void)setDisableScroll:(BOOL)disableScroll {
+ if (disableScroll == _disableScroll) {
+ return;
+ }
+ if (disableScroll) {
+ self.webView.scrollView.scrollEnabled = NO;
+ self.webView.scrollView.delegate = self;
+ }
+ else {
+ self.webView.scrollView.scrollEnabled = YES;
+ self.webView.scrollView.delegate = nil;
+ }
+
+ _disableScroll = disableScroll;
+}
+
+
+- (BOOL)hideKeyboardAccessoryBar {
+ return _hideKeyboardAccessoryBar;
+}
+
+- (void)setHideKeyboardAccessoryBar:(BOOL)hideKeyboardAccessoryBar {
+ if (hideKeyboardAccessoryBar == _hideKeyboardAccessoryBar) {
+ return;
+ }
+ if (hideKeyboardAccessoryBar) {
+ self.webView.hackishlyHidesInputAccessoryView = YES;
+ }
+ else {
+ self.webView.hackishlyHidesInputAccessoryView = NO;
+ }
+
+ _hideKeyboardAccessoryBar = hideKeyboardAccessoryBar;
+}
+
+/*
+- (BOOL)styleDark {
+ return _styleDark;
+}
+
+- (void)setStyleDark:(BOOL)styleDark {
+ if (styleDark == _styleDark) {
+ return;
+ }
+ if (styleDark) {
+ self.webView.styleDark = YES;
+ }
+ else {
+ self.webView.styleDark = NO;
+ }
+
+ _styleDark = styleDark;
+}
+*/
+
+
+/* ------------------------------------------------------------- */
+
+- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
+ [scrollView setContentOffset: CGPointZero];
+}
+
+/* ------------------------------------------------------------- */
+
+- (void)dealloc {
+ NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
+
+ [nc removeObserver:self name:UIKeyboardWillShowNotification object:nil];
+ [nc removeObserver:self name:UIKeyboardWillHideNotification object:nil];
+}
+
+/* ------------------------------------------------------------- */
+
+- (void) disableScroll:(CDVInvokedUrlCommand*)command {
+ if (!command.arguments || ![command.arguments count]){
+ return;
+ }
+ id value = [command.arguments objectAtIndex:0];
+
+ self.disableScroll = [value boolValue];
+}
+
+- (void) hideKeyboardAccessoryBar:(CDVInvokedUrlCommand*)command {
+ if (!command.arguments || ![command.arguments count]){
+ return;
+ }
+ id value = [command.arguments objectAtIndex:0];
+
+ self.hideKeyboardAccessoryBar = [value boolValue];
+}
+
+- (void) close:(CDVInvokedUrlCommand*)command {
+ [self.webView endEditing:YES];
+}
+
+- (void) show:(CDVInvokedUrlCommand*)command {
+ NSLog(@"Showing keyboard not supported in iOS due to platform limitations.");
+}
+
+/*
+- (void) styleDark:(CDVInvokedUrlCommand*)command {
+ if (!command.arguments || ![command.arguments count]){
+ return;
+ }
+ id value = [command.arguments objectAtIndex:0];
+
+ self.styleDark = [value boolValue];
+}
+*/
+
+@end
+
diff --git a/plugins/com.ionic.keyboard/src/ios/UIWebViewExtension.h b/plugins/com.ionic.keyboard/src/ios/UIWebViewExtension.h
new file mode 100644
index 00000000..1d6c293d
--- /dev/null
+++ b/plugins/com.ionic.keyboard/src/ios/UIWebViewExtension.h
@@ -0,0 +1,4 @@
+@interface UIWebView (HackishAccessoryHiding)
+@property (nonatomic, assign) BOOL hackishlyHidesInputAccessoryView;
+//@property (nonatomic, assign) BOOL styleDark;
+@end
diff --git a/plugins/com.ionic.keyboard/src/ios/UIWebViewExtension.m b/plugins/com.ionic.keyboard/src/ios/UIWebViewExtension.m
new file mode 100644
index 00000000..25403e6f
--- /dev/null
+++ b/plugins/com.ionic.keyboard/src/ios/UIWebViewExtension.m
@@ -0,0 +1,109 @@
+#import <objc/runtime.h>
+#import <UIKit/UIKit.h>
+#import "UIWebViewExtension.h"
+
+//Credit: https://gist.github.com/bjhomer/2048571
+//Also: http://stackoverflow.com/a/23398487/1091751
+@implementation UIWebView (HackishAccessoryHiding)
+
+static const char * const hackishFixClassName = "UIWebBrowserViewMinusAccessoryView";
+static Class hackishFixClass = Nil;
+
+- (UIView *)hackishlyFoundBrowserView {
+ UIScrollView *scrollView = self.scrollView;
+
+ UIView *browserView = nil;
+ for (UIView *subview in scrollView.subviews) {
+ if ([NSStringFromClass([subview class]) hasPrefix:@"UIWebBrowserView"]) {
+ browserView = subview;
+ break;
+ }
+ }
+ return browserView;
+}
+
+- (id)methodReturningNil {
+ return nil;
+}
+
+- (void)ensureHackishSubclassExistsOfBrowserViewClass:(Class)browserViewClass {
+ if (!hackishFixClass) {
+ Class newClass = objc_allocateClassPair(browserViewClass, hackishFixClassName, 0);
+ IMP nilImp = [self methodForSelector:@selector(methodReturningNil)];
+ class_addMethod(newClass, @selector(inputAccessoryView), nilImp, "@@:");
+ objc_registerClassPair(newClass);
+
+ hackishFixClass = newClass;
+ }
+}
+
+- (BOOL) hackishlyHidesInputAccessoryView {
+ UIView *browserView = [self hackishlyFoundBrowserView];
+ return [browserView class] == hackishFixClass;
+}
+
+- (void) setHackishlyHidesInputAccessoryView:(BOOL)value {
+ UIView *browserView = [self hackishlyFoundBrowserView];
+ if (browserView == nil) {
+ return;
+ }
+ [self ensureHackishSubclassExistsOfBrowserViewClass:[browserView class]];
+
+ if (value) {
+ object_setClass(browserView, hackishFixClass);
+ }
+ else {
+ Class normalClass = objc_getClass("UIWebBrowserView");
+ object_setClass(browserView, normalClass);
+ }
+ [browserView reloadInputViews];
+}
+/* ---------------------------------------------------------------- */
+
+/*
+- (UIKeyboardAppearance) darkKeyboardAppearanceTemplateMethod {
+ return UIKeyboardAppearanceDark;
+}
+
+- (UIKeyboardAppearance) lightKeyboardAppearanceTemplateMethod {
+ return UIKeyboardAppearanceLight;
+}
+
+- (BOOL) styleDark {
+ UIView *browserView = [self hackishlyFoundBrowserView];
+ if (browserView == nil) {
+ return false;
+ }
+
+ Method m = class_getInstanceMethod( [self class], @selector( darkKeyboardAppearanceTemplateMethod ) );
+ IMP imp = method_getImplementation( m );
+
+ Method m2 = class_getInstanceMethod( [browserView class], @selector(keyboardAppearance) );
+ IMP imp2 = method_getImplementation( m2 );
+
+ return imp == imp2;
+}
+
+- (void) setStyleDark:(BOOL)styleDark {
+ UIView *browserView = [self hackishlyFoundBrowserView];
+ if (browserView == nil) {
+ return;
+ }
+
+ if ( styleDark ) {
+ Method m = class_getInstanceMethod( [self class], @selector( darkKeyboardAppearanceTemplateMethod ) );
+ IMP imp = method_getImplementation( m );
+ const char* typeEncoding = method_getTypeEncoding( m );
+ class_replaceMethod( [browserView class], @selector(keyboardAppearance), imp, typeEncoding );
+ }
+ else {
+ Method m = class_getInstanceMethod( [self class], @selector( lightKeyboardAppearanceTemplateMethod ) );
+ IMP imp = method_getImplementation( m );
+ const char* typeEncoding = method_getTypeEncoding( m );
+ class_replaceMethod( [browserView class], @selector(keyboardAppearance), imp, typeEncoding );
+ }
+}
+*/
+
+@end
+