iOS 10.3新特性之动态替换App Icon

动态更换Icon

这里写图片描述
先看下iOS 10.3下新增的这三个属性

@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


第一个
supportsAlternateIcons 是否支持动态替换

第二个
也就是核心方法,根据配置,替换自己想要的icon,具体参数描述以及介绍大家基本上都知道,按下option进去看看就行了,主要还是涉及到info.plist的配置
官方指定的plist key介绍
这里写图片描述


第三个
替换icon的时候根据该属性获取到当前被替换到显示的icon名称,例如你没换的时候就是nil,换了icon1,那么就显示icon1



Plist配置以及Icon图片如何存放

第一步—>设置plist
首先看下info.plist,写的很详细了,依葫芦画瓢总会吧
这里写图片描述


第二步—>放iCON
首先和一个icon一样,也就是对应的Primary Icon,直接拖进Asset里面管理,需要注意的是,需要轮流替换的其他icon不能拖进去啊,不然跑起来,error了,直接和文件一样建个文件夹放在目录下即可,具体的字段和配置看一眼Demo搞一次就明白了,这里讲再多都没用


第三步—>代码

if (![[UIApplication sharedApplication] supportsAlternateIcons]) {
        NSLog(@"不支持。。。");
        return;
    }
    else
    {
        NSLog(@"支持动态替换");
    }

    NSString *icon = [[UIApplication sharedApplication] alternateIconName];
    if (icon) {
        NSLog(@"icon is exist");
        NSString *changeStr = nil;
        if ([icon isEqualToString:@"IconChange"]) {
            changeStr = @"IconChangeNext";
        }
        [[UIApplication sharedApplication] setAlternateIconName:changeStr completionHandler:^(NSError * _Nullable error) {
            if (error) {
                NSLog(@"error");
            }
            NSLog(@"done");
        }];
    }
    else
    {
        NSLog(@"icon not exist");
        [[UIApplication sharedApplication] setAlternateIconName:@"IconChange" completionHandler:^(NSError * _Nullable error) {
            if (error) {
                NSLog(@"error");
            }
            NSLog(@"done");
        }];
    }


一个特别奇怪的新特性,鬼知道apple要干嘛,难不成以后手机里面清一色一个icon,这样看起来也是很装B的。。。。。。



Demo地址

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值