iOS 动态修改App的应用图标

2020-11-13  本文已影响0人  032c6843a285

一、前言:

该功能应该是iOS10以后出的,相对于热度比较高的应用来说,根据业务需要,比如淘宝,双11需要动态修改icon,就可以借鉴此方法,话不多说,开始吧。

二、 优点于缺点:

优点

缺点

三、步骤

  • 准备至少2套图标(2x和3x),导入到项目中,不能放到.xcassets中。
  • 修改info.plist文件
image.png
1). CFBundleIcons: 一个字典,包含所有AppIcon信息,即上图的Icon files(iOS 5)
2). CFBundlePrimaryIcon:如果已经在Assets.xcassets中设置了AppIcon,那么CFBundlePrimaryIcon中
的配置会被忽略,Assets.xcassets的AppIcon将会自动配置到CFBundlePrimaryIcon中。
3). CFBundleAlternateIcons: 一个数组,负责配置可供替换的icon信息
4). UIPrerenderedIcon: 是否已经预渲染,如果不设置该项或者设为NO。系统会自动为icon进行渲染增
加光泽

四、查看系统ipa接口

// 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));
- (void)setAlternateIconName:(nullable NSString *)alternateIconName completionHandler:(nullable void (^)(NSError *_Nullable error))completionHandler

方法中的alternateIconName参数,是要填写您在Info.plist 中填写的名字,如图二中所示,我们应当在此方法中填写Icon-60或者Icon-76.

五、遇到问题的解决方案

按照上面的方法能够动态更换Icon,但是在更换成功后,系统会又个弹窗提示用户,Icon更换成功,那么如何不让弹窗弹出来呢?
把调用setAlternateIconName的代码注释,换成如下代码。

if ([[UIApplication sharedApplication] respondsToSelector:@selector(supportsAlternateIcons)] &&
         [[UIApplication sharedApplication] supportsAlternateIcons])
     {
         NSMutableString *selectorString = [[NSMutableString alloc] initWithCapacity:40];
         [selectorString appendString:@"_setAlternate"];
         [selectorString appendString:@"IconName:"];
         [selectorString appendString:@"completionHandler:"];
         
         SEL selector = NSSelectorFromString(selectorString);
         IMP imp = [[UIApplication sharedApplication] methodForSelector:selector];
         void (*func)(id, SEL, id, id) = (void *)imp;
         if (func)
         {
             func([UIApplication sharedApplication], selector, iconName, ^(NSError * _Nullable error) {});
         }
     }

写在最后 ⚠️

上一篇 下一篇

猜你喜欢

热点阅读