Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15747 anikendra 1
#import <objc/runtime.h>
2
#import <UIKit/UIKit.h>
3
#import "UIWebViewExtension.h"
4
 
5
//Credit: https://gist.github.com/bjhomer/2048571
6
//Also: http://stackoverflow.com/a/23398487/1091751
7
@implementation UIWebView (HackishAccessoryHiding)
8
 
9
static const char * const hackishFixClassName = "UIWebBrowserViewMinusAccessoryView";
10
static Class hackishFixClass = Nil;
11
 
12
- (UIView *)hackishlyFoundBrowserView {
13
    UIScrollView *scrollView = self.scrollView;
14
 
15
    UIView *browserView = nil;
16
    for (UIView *subview in scrollView.subviews) {
17
        if ([NSStringFromClass([subview class]) hasPrefix:@"UIWebBrowserView"]) {
18
            browserView = subview;
19
            break;
20
        }
21
    }
22
    return browserView;
23
}
24
 
25
- (id)methodReturningNil {
26
    return nil;
27
}
28
 
29
- (void)ensureHackishSubclassExistsOfBrowserViewClass:(Class)browserViewClass {
30
    if (!hackishFixClass) {
31
        Class newClass = objc_allocateClassPair(browserViewClass, hackishFixClassName, 0);
32
        IMP nilImp = [self methodForSelector:@selector(methodReturningNil)];
33
        class_addMethod(newClass, @selector(inputAccessoryView), nilImp, "@@:");
34
        objc_registerClassPair(newClass);
35
 
36
        hackishFixClass = newClass;
37
    }
38
}
39
 
40
- (BOOL) hackishlyHidesInputAccessoryView {
41
    UIView *browserView = [self hackishlyFoundBrowserView];
42
    return [browserView class] == hackishFixClass;
43
}
44
 
45
- (void) setHackishlyHidesInputAccessoryView:(BOOL)value {
46
    UIView *browserView = [self hackishlyFoundBrowserView];
47
    if (browserView == nil) {
48
        return;
49
    }
50
    [self ensureHackishSubclassExistsOfBrowserViewClass:[browserView class]];
51
 
52
    if (value) {
53
        object_setClass(browserView, hackishFixClass);
54
    }
55
    else {
56
        Class normalClass = objc_getClass("UIWebBrowserView");
57
        object_setClass(browserView, normalClass);
58
    }
59
    [browserView reloadInputViews];
60
}
61
/* ---------------------------------------------------------------- */
62
 
63
/*
64
- (UIKeyboardAppearance) darkKeyboardAppearanceTemplateMethod {
65
    return UIKeyboardAppearanceDark;
66
}
67
 
68
- (UIKeyboardAppearance) lightKeyboardAppearanceTemplateMethod {
69
    return UIKeyboardAppearanceLight;
70
}
71
 
72
- (BOOL) styleDark {
73
    UIView *browserView = [self hackishlyFoundBrowserView];
74
    if (browserView == nil) {
75
      return false;
76
    }
77
 
78
    Method m = class_getInstanceMethod( [self class], @selector( darkKeyboardAppearanceTemplateMethod ) );
79
    IMP imp = method_getImplementation( m );
80
 
81
    Method m2 = class_getInstanceMethod( [browserView class], @selector(keyboardAppearance) );
82
    IMP imp2 = method_getImplementation( m2 );
83
 
84
    return imp == imp2;
85
}
86
 
87
- (void) setStyleDark:(BOOL)styleDark {
88
    UIView *browserView = [self hackishlyFoundBrowserView];
89
    if (browserView == nil) {
90
        return;
91
    }
92
 
93
    if ( styleDark ) {
94
      Method m = class_getInstanceMethod( [self class], @selector( darkKeyboardAppearanceTemplateMethod ) );
95
      IMP imp = method_getImplementation( m );
96
      const char* typeEncoding = method_getTypeEncoding( m );
97
      class_replaceMethod( [browserView class], @selector(keyboardAppearance), imp, typeEncoding );
98
    }
99
    else {
100
      Method m = class_getInstanceMethod( [self class], @selector( lightKeyboardAppearanceTemplateMethod ) );
101
      IMP imp = method_getImplementation( m );
102
      const char* typeEncoding = method_getTypeEncoding( m );
103
      class_replaceMethod( [browserView class], @selector(keyboardAppearance), imp, typeEncoding );
104
    }
105
}
106
*/
107
 
108
@end
109