forked from illusionspaces/WKJavaScriptBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWKPluginResult.m
More file actions
63 lines (50 loc) · 2.38 KB
/
WKPluginResult.m
File metadata and controls
63 lines (50 loc) · 2.38 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
//
// WKPluginResult.m
// Hybrid-framework
//
// Created by Kevin on 2019/4/23.
// Copyright © 2019 王凯. All rights reserved.
//
#import "WKPluginResult.h"
@interface WKPluginResult ()
@property (nonatomic, strong, readwrite) NSNumber* status;
@property (nonatomic, strong, readwrite) id message;
@end
@implementation WKPluginResult
+ (WKPluginResult *)resultWithStatus:(WKCommandStatus)statusOrdinal messageAsDictionary:(NSDictionary *)theMessage {
return [[self alloc] initWithStatus:statusOrdinal message:theMessage];
}
+ (WKPluginResult *)resultWithStatus:(WKCommandStatus)statusOrdinal messageAsString:(NSString *)theMessage {
return [[self alloc] initWithStatus:statusOrdinal message:theMessage];
}
- (WKPluginResult*)initWithStatus:(WKCommandStatus)statusOrdinal message:(id)theMessage {
self = [super init];
if (self) {
self.status = [NSNumber numberWithInt:statusOrdinal];
self.message = theMessage;
}
return self;
}
+ (NSString *)jsSerializeWithJson:(NSDictionary * _Nullable)json {
NSString *messageJSON = [self serializeWithJson:json ? json : @{} pretty:NO];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\'" withString:@"\\\'"];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\r" withString:@"\\r"];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\f" withString:@"\\f"];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\u2028" withString:@"\\u2028"];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\u2029" withString:@"\\u2029"];
return messageJSON;
}
+ (NSString *)serializeWithJson:(NSDictionary * _Nullable)json pretty:(BOOL)pretty {
NSError *error = nil;
NSString *str = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:json ? json : @{} options:(NSJSONWritingOptions)(pretty ? NSJSONWritingPrettyPrinted : 0) error:&error] encoding:NSUTF8StringEncoding];
#ifdef DEBUG
if (error) {
NSLog(@"WKJSBridge Error: format json error %@", error.localizedDescription);
}
#endif
return str ? str : @"";
}
@end