iOS之更换应用程序的图标(iOS10 3之后)

最近iOS10.3更新了,然后有个好玩的功能,就是可以更新应用图标了。

API文档

下面来看看最近的iOS10.3关于icon的API

@interface UIApplication (UIAlternateApplicationIcons)
// If false, alternate icons are not supported for the current process.
@property (readonly, nonatomic) BOOL supportsAlternateIcons NS_EXTENSION_UNAVAILABLE("Extensions may not have alternate icons") API_AVAILABLE(ios(10.3), tvos(10.2));

// 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.
- (void)setAlternateIconName:(nullable NSString *)alternateIconName completionHandler:(nullable void (^)(NSError *_Nullable error))completionHandler NS_EXTENSION_UNAVAILABLE("Extensions may not have alternate icons") API_AVAILABLE(ios(10.3), tvos(10.2));

// If `nil`, the primary application icon is being used.
@property (nullable, readonly, nonatomic) NSString *alternateIconName NS_EXTENSION_UNAVAILABLE("Extensions may not have alternate icons") API_AVAILABLE(ios(10.3), tvos(10.2));
@end
复制代码

实现方案

上代码: 首先得判断系统是否支持更换图标(文档中有一个BOOL的属性,就是判断是否支持的) [UIApplication sharedApplication].supportsAlternateIcons 然后在做图标的更换。

if ([UIApplication sharedApplication].alternateIconName) {
        [[UIApplication sharedApplication] setAlternateIconName:nil completionHandler:^(NSError * _Nullable error) {
            NSLog(@"the alternate icon's error is:%@", error.description);
        }];
  } else {
        [[UIApplication sharedApplication] setAlternateIconName:@"blackBgColor" completionHandler:^(NSError * _Nullable error) {
            NSLog(@"the alternate icon's error is:%@", error);
        }];
  }
复制代码

其中传入的nil,默认代表的是最初始的那张图片(文档中有相关的解释)。

运行报错时

运行的时候,点击切换按钮,通过打印的error的日志得到的是CFBundleAlternateIcons的缺失,所以需要在plist文件中进行设置。

代码:连接

添加了一个swift的类,和oc的一样的功能。酌情删除OC类或者swift类就可以使用(storyBoard的连线需要重新设置)。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值