forked from H5GG/H5GG
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTweak.mm
More file actions
139 lines (106 loc) · 4.84 KB
/
Copy pathTweak.mm
File metadata and controls
139 lines (106 loc) · 4.84 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//为了保持未来版本兼容性, 请勿使用JSContext
//To maintain compatibility with future versions, do not use JSContext
#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import <JavaScriptCore/JavaScriptCore.h>
#import <UIKit/UIKit.h>
#pragma GCC diagnostic ignored "-Wunguarded-availability-new"
//定义JS函数接口
@protocol MyJSExport <JSExport>
-(void)alert0; //无参数直接定义
-(void)alert1:(NSString*)msg; //一个参数直接定义
JSExportAs(alert2, -(void)alert2:(NSString*)title msg:(NSString*)msg); //超过一个参数用JSExportAs定义
JSExportAs(choice, -(void)choice:(NSArray*)items callback:(JSValue*)jsfunc); //超过一个参数用JSExportAs定义
@end
//定义插件类
@interface MyAlert : NSObject <MyJSExport>
@property UIWindow* myWindow;
@property JSValue* jscallback;
@property NSThread* jsthread;
@end
//实现插件接口函数
@implementation MyAlert
-(instancetype)init {
if (self = [super init])
{
dispatch_async(dispatch_get_main_queue(), ^{
if (NSClassFromString(@"UIWindowScene")) {
UIWindowScene* theScene=nil;
for (UIWindowScene* windowScene in [UIApplication sharedApplication].connectedScenes) {
if(!theScene && windowScene.activationState==UISceneActivationStateForegroundInactive)
theScene = windowScene;
if (windowScene.activationState == UISceneActivationStateForegroundActive) {
theScene = windowScene;
break;
}
}
self.myWindow = [[UIWindow alloc] initWithWindowScene:theScene];
}else{
self.myWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
}
self.myWindow.windowLevel = UIWindowLevelAlert;
self.myWindow.rootViewController = [[UIViewController alloc] init];
});
}
return self;
}
-(void)threadcall:(void(^)())block {
block();
}
-(void)choice:(NSArray*)items callback:(JSValue*)jsfunc
{
self.jscallback = jsfunc;
self.jsthread = NSThread.currentThread;
//通过主线程执行下面的代码
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请选择" preferredStyle:UIAlertControllerStyleAlert];
for(NSString* item in items)
{
[alert addAction:[UIAlertAction actionWithTitle:item style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self.myWindow setHidden:YES];
[self performSelector:@selector(threadcall:) onThread:self.jsthread withObject:^{
[self.jscallback callWithArguments:@[item]];
} waitUntilDone:NO];
}]];
}
[self.myWindow setHidden:NO];
[[self.myWindow rootViewController] presentViewController:alert animated:YES completion:nil];
});
}
-(void)alert0
{
//通过主线程执行下面的代码
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"来自OC的问候" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self.myWindow setHidden:YES];
}]];
[self.myWindow setHidden:NO];
[[self.myWindow rootViewController] presentViewController:alert animated:YES completion:nil];
});
}
-(void)alert1:(NSString*)msg
{
//通过主线程执行下面的代码
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self.myWindow setHidden:YES];
}]];
[self.myWindow setHidden:NO];
[[self.myWindow rootViewController] presentViewController:alert animated:YES completion:nil];
});
}
-(void)alert2:(NSString*)title msg:(NSString*)msg
{
//通过主线程执行下面的代码
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self.myWindow setHidden:YES];
}]];
[self.myWindow setHidden:NO];
[[self.myWindow rootViewController] presentViewController:alert animated:YES completion:nil];
});
}
@end