PGPluginTest.m
4.13 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
//
// PluginTest.m
// HBuilder-Hello
//
// Created by Mac Pro on 14-9-3.
// Copyright (c) 2014年 DCloud. All rights reserved.
//
#import "PGPluginTest.h"
@implementation PGPluginTest
- (void)PluginTestFunction:(PGMethod*)commands
{
if ( commands ) {
// CallBackid 异步方法的回调id,H5+ 会根据回调ID通知JS层运行结果成功或者失败
NSString* cbId = [commands.arguments objectAtIndex:0];
// 用户的参数会在第二个参数传回
NSString* pArgument1 = [commands.arguments objectAtIndex:1];
NSString* pArgument2 = [commands.arguments objectAtIndex:2];
NSLog(@"%@",commands.arguments);
[HHMSDK.default_ loginWithUserToken:pArgument2 completion:^(NSString * _Nullable result) {
if (result == nil) {
[HHMSDK.default_ skipChatHomeWithSkipType:SkipTypePresent vc:[self getCurrentVC]];
}
}];
}
}
//获取当前屏幕显示的viewcontroller
- (UIViewController *)getCurrentVC{
UIViewController *aRoot = [[[UIApplication sharedApplication] keyWindow] rootViewController];
while (aRoot.presentedViewController != nil) {
aRoot = aRoot.presentedViewController;
if ([aRoot isKindOfClass:UINavigationController.class]) {
aRoot = ((UINavigationController *) aRoot).visibleViewController;
}else if ([aRoot isKindOfClass:UITabBarController.class]) {
aRoot = ((UITabBarController *) aRoot).selectedViewController;
}
}
if ([aRoot isKindOfClass:UINavigationController.class]) {
aRoot = ((UINavigationController *) aRoot).visibleViewController;
}
return aRoot;
}
- (void)PluginTestFunctionArrayArgu:(PGMethod*)commands
{
if ( commands ) {
// CallBackid 异步方法的回调id,H5+ 会根据回调ID通知JS层运行结果成功或者失败
NSString* cbId = [commands.arguments objectAtIndex:0];
// 用户的参数会在第二个参数传回,可以按照Array方式传入,
NSArray* pArray = [commands.arguments objectAtIndex:1];
// 如果使用Array方式传递参数
NSString* pResultString = [NSString stringWithFormat:@"%@ %@ %@ %@",[pArray objectAtIndex:0], [pArray objectAtIndex:1], [pArray objectAtIndex:2], [pArray objectAtIndex:3]];
// 运行Native代码结果和预期相同,调用回调通知JS层运行成功并返回结果
PDRPluginResult *result = [PDRPluginResult resultWithStatus:PDRCommandStatusOK messageAsString:pResultString];
// 如果Native代码运行结果和预期不同,需要通过回调通知JS层出现错误,并返回错误提示
//PDRPluginResult *result = [PDRPluginResult resultWithStatus:PDRCommandStatusError messageAsString:@"惨了! 出错了! 咋(wu)整(liao)"];
// 通知JS层Native层运行结果
[self toCallback:cbId withReslut:[result toJSONString]];
}
}
- (NSData*)PluginTestFunctionSync:(PGMethod*)command
{
// 根据传入获取参数
NSString* pArgument1 = [command.arguments objectAtIndex:0];
NSString* pArgument2 = [command.arguments objectAtIndex:1];
NSString* pArgument3 = [command.arguments objectAtIndex:2];
NSString* pArgument4 = [command.arguments objectAtIndex:3];
// 拼接成字符串
NSString* pResultString = [NSString stringWithFormat:@"%@ %@ %@ %@", pArgument1, pArgument2, pArgument3, pArgument4];
// 按照字符串方式返回结果
return [self resultWithString: pResultString];
}
- (NSData*)PluginTestFunctionSyncArrayArgu:(PGMethod*)command
{
// 根据传入参数获取一个Array,可以从中获取参数
NSArray* pArray = [command.arguments objectAtIndex:0];
// 创建一个作为返回值的NSDictionary
NSDictionary* pResultDic = [NSDictionary dictionaryWithObjects:pArray forKeys:[NSArray arrayWithObjects:@"RetArgu1",@"RetArgu2",@"RetArgu3", @"RetArgu4", nil]];
// 返回类型为JSON,JS层在取值是需要按照JSON进行获取
return [self resultWithJSON: pResultDic];
}
@end