iOS端游戏 动态更换AppIcon

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的动作,或者把这个方法放入主队列来调用,不然该方法没有反应。
以上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值