iOS端游戏 动态更换AppIcon
因为手游基本都是热更新,通过AppStore 更新版本的时间间隔很长,但是有一些活动需要更换icon,所以有了这个需求。
首先把1024*1024分辨率icon放入文件夹中。(需要注意的是 图片资源需要放在外边,不能放在asset资源里面。)
info.plist文件中加入相应参数:(Primary Icon 字段设置为AppIcon(就是asset里面的名字),如果设置appicon的API的name参数设置为nil,会自动读取Primary Icon对应的图片)
代码如下:
<key>CFBundleIcons</key>
<dict>
<key>CFBundleAlternateIcons</key>
<dict>
<key>Cynthia</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>Cynthia</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
<key>mai</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>mai</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
</dict>
<key>CFBundlePrimaryIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string></string>
</array>
<key>CFBundleIconName</key>
<string></string>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
调用系统方法:
- (void)changeAppIconWithName:(NSString *)iconName {
if (![[UIApplication sharedApplication] supportsAlternateIcons]) {
return;
}
if ([iconName isEqualToString:@""]) {
iconName = nil;
}
[[UIApplication sharedApplication] setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"更换app图标发生错误了 : %@",error);
}
}];
}
这个系统方法的注释:
Pass
nil
to use the primary application icon. The completion handler will be invoked asynchronously on an arbitrary background queue; be sure to dispatch back to the main queue before doing any further UI work
官方注释:
要保证这个方法调用时没有其他更新UI的动作,或者把这个方法放入主队列来调用,不然该方法没有反应。
以上。