-
Notifications
You must be signed in to change notification settings - Fork 58
/
EasyJSDataFunction.m
57 lines (44 loc) · 1.38 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
//
// EasyJSDataFunction.m
// EasyJSWebViewSample
//
// Created by Alex Lau on 21/1/13.
// 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){
for (int i = 0, l = params.count; i < l; i++){
NSString* arg = [params objectAtIndex:i];
NSString* encodedArg = (NSString*) CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)arg, NULL, (CFStringRef) @"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8);
[injection appendFormat:@", \"%@\"", encodedArg];
}
}
[injection appendString:@");"];
if (self.webView){
return [self.webView stringByEvaluatingJavaScriptFromString:injection];
}else{
return nil;
}
}
@end