CDVYHInterface.m
5.55 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#import "CDVYHInterface.h"
#import "NSDictionary+Extensions.h"
#import "CDVYHInterfaceDelegate.h"
#import "YHNative.h"
@interface CDVYHInterface ()
@property (strong, nonatomic) NSMutableDictionary *abilities;
@end
@implementation CDVYHInterface
- (NSMutableDictionary *)abilities
{
_abilities = _abilities?:[[NSMutableDictionary alloc] initWithCapacity:1];
return _abilities;
}
- (void)dealloc
{
}
- (void)eventTriggered:(CDVInvokedUrlCommand *)command
{
__weak typeof(self)weakSelf = self;
id <CDVYHInterfaceDelegate> yhif = nil;
if ([self.viewController conformsToProtocol:@protocol(CDVYHInterfaceDelegate)]) {
yhif = (id <CDVYHInterfaceDelegate>)self.viewController;
}
__weak typeof(yhif) weakIf = yhif;
[self.commandDelegate runInBackground:^{
NSDictionary *info = [command argumentAtIndex:0 withDefault:nil];
if (!info) {
return;
}
if (info.allKeys.count < 1) {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Unknown Event"];
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
NSDictionary *options = nil;
Class abilityClass = nil;
SEL action = nil;
NSString *ability = nil;
for (NSString *key in info.allKeys) {
if ([key isEqualToString:@"options"]) {
options = info[key];
} else {
if (!abilityClass) {
ability = key;
abilityClass = [YHNative resolveActionName:key];
if (abilityClass) {
action = NSSelectorFromString(info[key]);
}
}
}
}
if (!abilityClass || !action) {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Unknown Event"];
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
YHNative *native = weakSelf.abilities[NSStringFromClass(abilityClass)];
if (!native) {
native = [[abilityClass alloc] init];
weakSelf.abilities[NSStringFromClass(abilityClass)] = native;
}
native.originalParams = info;
BOOL canProcess = YES;
if (weakIf && [weakIf respondsToSelector:@selector(nativeActionShouldBegin:)]) {
canProcess = [weakIf nativeActionShouldBegin:native];
}
if (!canProcess) {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT messageAsString:@"Not permitted"];
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
return;
}
__weak typeof(native) wn = native;
[native workWithMethod:action options:options success:^(NSDictionary *info, BOOL keepCallback, NSDictionary *nativeInfo) {
NSDictionary *retDict = nil;
if (info) {
retDict = @{ @"ability": ability, @"action": NSStringFromSelector(action), @"result": info };
} else {
retDict = @{ @"ability": ability, @"action": NSStringFromSelector(action), @"result": @{} };
}
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:retDict];
pluginResult.keepCallback = @(keepCallback);
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
if (weakIf && [weakIf respondsToSelector:@selector(nativeAction:didEndWithResult:userInfo:)]) {
dispatch_async(dispatch_get_main_queue(), ^{
[weakIf nativeAction:wn didEndWithResult:YES userInfo:nativeInfo];
});
}
} failure:^(NSString *message) {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:message];
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
if (weakIf && [weakIf respondsToSelector:@selector(nativeAction:didEndWithResult:userInfo:)]) {
dispatch_async(dispatch_get_main_queue(), ^{
[weakIf nativeAction:wn didEndWithResult:NO userInfo:nil];
});
}
}];
}];
}
- (BOOL)shouldOverrideLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *urlString = request.URL.absoluteString;
switch (navigationType) {
case UIWebViewNavigationTypeLinkClicked:
{
// 先检查是否遵循CDVYHInterfaceDelegate
id <CDVYHInterfaceDelegate> yhif = nil;
if ([self.viewController conformsToProtocol:@protocol(CDVYHInterfaceDelegate)]) {
yhif = (id <CDVYHInterfaceDelegate>)self.viewController;
} else {
break;
}
return [yhif parseLink:urlString];
break;
}
// case UIWebViewNavigationTypeFormSubmitted:
// case UIWebViewNavigationTypeBackForward:
// case UIWebViewNavigationTypeReload:
// case UIWebViewNavigationTypeFormResubmitted:
// case UIWebViewNavigationTypeOther:
default:
break;
}
return NO;
}
#pragma mark - Private
//- (void)
@end