summaryrefslogtreecommitdiff
path: root/plugins/com.ionic.keyboard/src/ios/UIWebViewExtension.m
blob: 25403e6f98d7bcc8df04a47336cd28d0434e8641 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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