CustomIcon.m
1.7 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
#import "CustomIcon.h"
#import <UIKit/UIKit.h>
@implementation CustomIcon
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(changeIcon:(NSString *)iconName) {
UIApplication *app = [UIApplication sharedApplication];
dispatch_sync(dispatch_get_main_queue(), ^{
if (![[UIApplication sharedApplication] supportsAlternateIcons]) {
return;
}
});
if ([iconName isEqualToString:@""]) {
iconName = nil;
}
dispatch_sync(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"更换app图标发生错误了 : %@",error);
}
}];
});
}
// 恢复默认图标
RCT_EXPORT_METHOD(changeDefaultIcon) {
UIApplication *app = [UIApplication sharedApplication];
dispatch_sync(dispatch_get_main_queue(), ^{
if (![[UIApplication sharedApplication] supportsAlternateIcons]) {
return;
}
});
dispatch_sync(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] setAlternateIconName:nil completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"更换app图标发生错误了 : %@",error);
}
}];
});
}
// 获取当前图标
RCT_EXPORT_METHOD(getIconName:(RCTResponseSenderBlock) callback ){
NSString *name = @"default";
NSDictionary *results;
if( [[UIApplication sharedApplication] supportsAlternateIcons ] ){
name = [[UIApplication sharedApplication] alternateIconName];
if( name == nil ){
name = @"default";
}
}
results = name;
callback(@[results]);
}
@end