CDVYHInterface.m
5.14 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
#import "CDVYHInterface.h"
#import "CDVYHInterfaceDelegate.h"
#import <objc/runtime.h>
#import "WebPProtocol.h"
@interface CDVYHInterface ()
@property (strong, nonatomic) NSMutableDictionary *abilities;
@end
@implementation CDVYHInterface
- (NSMutableDictionary *)abilities
{
_abilities = _abilities?:[[NSMutableDictionary alloc] initWithCapacity:1];
return _abilities;
}
- (void)pluginInitialize {
[NSURLProtocol registerClass:[WebPProtocol class]];
}
- (void)dispose {
[super dispose];
[NSURLProtocol unregisterClass:[WebPProtocol class]];
}
- (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 || ![info isKindOfClass:[NSDictionary class]]) {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Invalid parameter format!"];
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
return;
}
if (info.allKeys.count < 1) {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Unknown Event"];
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
return;
}
if ([weakIf respondsToSelector:@selector(nativeActionDidTriggerEventWithUserInfo:completion:)]) {
[weakIf nativeActionDidTriggerEventWithUserInfo:info
completion:^(id callbackInfo, BOOL result) {
CDVPluginResult *pluginResult;
CDVCommandStatus cmdStatus = CDVCommandStatus_ERROR;
if (result) {
cmdStatus = CDVCommandStatus_OK;
} else {
cmdStatus = CDVCommandStatus_ERROR;
}
if ([callbackInfo isKindOfClass:[NSString class]]) {
pluginResult = [CDVPluginResult resultWithStatus:cmdStatus messageAsString:(NSString *)callbackInfo];
}else if ([callbackInfo isKindOfClass:[NSDictionary class]]){
pluginResult = [CDVPluginResult resultWithStatus:cmdStatus
messageAsDictionary:(NSDictionary *)callbackInfo];
}else if ([callbackInfo isKindOfClass:[NSArray class]]){
pluginResult = [CDVPluginResult resultWithStatus:cmdStatus
messageAsMultipart:(NSArray *)callbackInfo];
}
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
} else {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:nil];
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
}];
}
- (BOOL)shouldOverrideLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
BOOL result = YES;
NSString *urlString = request.URL.absoluteString;
switch (navigationType) {
case UIWebViewNavigationTypeLinkClicked:
case UIWebViewNavigationTypeOther:
{
// 先检查是否遵循CDVYHInterfaceDelegate
id <CDVYHInterfaceDelegate> yhif = nil;
if ([self.viewController conformsToProtocol:@protocol(CDVYHInterfaceDelegate)]) {
yhif = (id <CDVYHInterfaceDelegate>)self.viewController;
result = ![yhif parseLink:urlString];
}
}
break;
// case UIWebViewNavigationTypeFormSubmitted:
// case UIWebViewNavigationTypeBackForward:
// case UIWebViewNavigationTypeReload:
// case UIWebViewNavigationTypeFormResubmitted:
default:
break;
}
return result;
}
#pragma mark - Private
@end