forked from dukeland/EasyJSWebView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EasyJSDataFunction.m
67 lines (55 loc) · 1.99 KB
/
EasyJSDataFunction.m
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
//
// EasyJSDataFunction.m
// EasyJSWebViewSample
//
// Created by Alex Lau on 21/1/13.
// Modified by 腹黒い茶 on 2/3/2015.
// Copyright (c) 2013 Dukeland. All rights reserved.
//
#import "EasyJSDataFunction.h"
@implementation EasyJSDataFunction
@synthesize funcID;
@synthesize webView;
@synthesize removeAfterExecute;
- (id)initWithWebView:(EasyJSWebView *)_webView{
self = [super init];
if (self) {
self.webView = _webView;
}
return self;
}
- (NSString *)execute {
return [self executeWithParams:nil];
}
- (NSString *)executeWithParam:(NSString *)param {
NSMutableArray *params = [[NSMutableArray alloc] initWithObjects:param, nil];
return [self executeWithParams:params];
}
- (NSString *)executeWithParams:(NSArray *)params {
NSMutableString *injection = [[NSMutableString alloc] init];
[injection appendFormat:@"EasyJS.invokeCallback(\"%@\", %@", self.funcID, self.removeAfterExecute ? @"true" : @"false"];
if (params) {
NSString *args = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:params
options:NSJSONWritingPrettyPrinted
error:nil]
encoding:NSUTF8StringEncoding];
[injection appendFormat:@", JSON.stringify(%@)", args];
}
[injection appendString:@");"];
__block id returnObject = nil;
if (self.webView) {
__block BOOL returned = NO;
[self.webView evaluateJavaScript:injection completionHandler:^(id obj, NSError *error) {
returned = YES;
returnObject = obj;
}];
while (!returned) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1f]];
}
}
if (self.callbackAfterExecuteAction != nil) {
[self callbackAfterExecuteAction];
}
return returnObject;
}
@end