iOS动态更改icon图标

1.将所有@2x,@3x图标放到项目中

2.配置info.plist

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
<key>CFBundleIcons</key>
<dict>
<key>CFBundlePrimaryIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>appIcon1</string>
</array>
</dict>
<key>CFBundleAlternateIcons</key>
<dict>
<key>appIcon1</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>appIcon1</string>
</array>
<key>UIPrerenderedIcon</key>
<string>NO</string>
</dict>

<key>appIcon2</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>appIcon2</string>
</array>
<key>UIPrerenderedIcon</key>
<string>NO</string>
</dict>

<key>appIcon3</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>appIcon3</string>
</array>
<key>UIPrerenderedIcon</key>
<string>NO</string>
</dict>
</dict>
</dict>

3.更换图标(仅限于iOS版本10.3以上)

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
- (void)changeIcon{

if ([UIApplication sharedApplication].supportsAlternateIcons) {
NSLog(@"you can change this app's icon");
}else{
NSLog(@"you can not change this app's icon");
return;
}

NSString *iconName = [[UIApplication sharedApplication] alternateIconName];
NSLog(@"%@",iconName);
if (iconName) {
// change to primary icon
[[UIApplication sharedApplication] setAlternateIconName:@"appIcon1" completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"set icon error: %@",error);
}
NSLog(@"The alternate icon's name is %@",iconName);
}];
}else{
// change to alterante icon
[[UIApplication sharedApplication] setAlternateIconName:@"appIcon2" completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"set icon error: %@",error);
}
NSLog(@"The alternate icon's name is %@",iconName);
}];
}
}

-------------本文结束感谢您的阅读-------------