CNLIVEViewController.m
4.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
//
// CNLIVEViewController.m
// CNLiveNewIMAManagerKit
//
// Created by iOSniuniu on 05/25/2021.
// Copyright (c) 2021 iOSniuniu. All rights reserved.
//
#import "CNLIVEViewController.h"
#import <CNLiveNewIMAManagerKit/CNLiveNewIMAManagerKit.h>
@interface CNLIVEViewController ()
@end
@implementation CNLIVEViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 50, 100, 50);
[btn setTitle:@"存数据" forState:UIControlStateNormal];
btn.backgroundColor = [UIColor redColor];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(setAction) forControlEvents:UIControlEventTouchUpInside];
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(0, 150, 100, 50);
[btn1 setTitle:@"取数据" forState:UIControlStateNormal];
btn1.backgroundColor = [UIColor redColor];
[self.view addSubview:btn1];
[btn1 addTarget:self action:@selector(getAction) forControlEvents:UIControlEventTouchUpInside];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)setAction
{
// [[CNTransferBalanceUnreceivedCache cache] addOneElement:@{@"sourceId": @"1222",
// @"fromId" : @"1111",
// @"type" : @"1",
// } forKey:@"key1"];
}
- (void)getAction
{
// NSArray *array = [[CNTransferBalanceUnreceivedCache cache] objectForKey:@"key1"];
// NSLog(@"array = %@",array);
// 创建 JSVirtualMachine 对象 jsvmJSVirtualMachine *jsvm = [[JSVirtualMachine alloc] init];// 使用 jsvm 的 JSContext 对象 ctJSContext *ct = [[JSContext alloc] initWithVirtualMachine:jsvm];
JSVirtualMachine *jsvm = [[JSVirtualMachine alloc] init];
JSContext *context = [[JSContext alloc] initWithVirtualMachine:jsvm];
// JSContext *context = [[JSContext alloc] init];
// 解析执行 JavaScript 脚本[context evaluateScript:@"var i = 4 + 8"];// 转换 i 变量为原生对象NSNumber *number = [context[@"i"] toNumber];NSLog(@"var i is %@, number is %@",context[@"i"], number);
[context evaluateScript:@"var a = 4 + 8"];
NSNumber *number = [context[@"a"] toNumber];
NSInteger a = [number integerValue];
NSLog(@"var i is %@, number is %@",context[@"a"], number);
// 解析执行 JavaScript 脚本[context evaluateScript:@"function addition(x, y) { return x + y}"];// 获得 addition 函数JSValue *addition = context[@"addition"];// 传入参数执行 addition 函数JSValue *resultValue = [addition callWithArguments:@[@(4), @(8)]];// 将 addition 函数执行的结果转成原生 NSNumber 来使用。NSLog(@"function is %@; reslutValue is %@",addition, [resultValue toNumber]);
[context evaluateScript:@"function addition(x, y) {}"];
JSValue *addition = context[@"addition"];
id fun = [addition toObject];
if ([addition isDate]) {
printf("[addition isDate]");
}
if ([addition isUndefined]) {
printf("isUndefined");
}
if ([addition isNull]) {
printf("null");
}
if ([addition isNumber]) {
printf("isNumber");
}
if ([addition isString]) {
printf("isString");
}
if ([addition isBoolean]) {
printf("isBoolean");
}
if ([addition isObject]) {
printf("isObject");
}
if ([addition isArray]) {
printf("isArray");
}
if ([addition isSymbol]) {
printf("isSymbol");
}
JSValue *resultValue = [addition callWithArguments:@[@(4),@(8)]];
NSNumber *sumNumber = [resultValue toNumber];
NSInteger sum = [sumNumber integerValue];
NSLog(@"function is %@; reslutValue is %@",addition, [resultValue toNumber]);
[[context globalObject] invokeMethod:@"" withArguments:@[]];
// - (JSValue *)callJSMethod:(NSString *)method args:(NSArray *)args {
// WXLogDebug(@"Calling JS... method:%@, args:%@", method, args);
// return [[_jsContext globalObject] invokeMethod:method withArguments:args];
//
// }
// 在 JSContext 中使用原生 Block 设置一个减法 subtraction 函数context[@"subtraction"] = ^(int x, int y) { return x - y;};// 在同一个 JSContext 里用 JavaScript 代码来调用原生 subtraction 函数JSValue *subValue = [context evaluateScript:@"subtraction(4,8);"];NSLog(@"substraction(4,8) is %@",[subValue toNumber]);
}
@end