- 应用说明
关于节日时,app安装在手机桌面展示的icon变化处理,
iOS10.3以后可通过设置setAlternateIconName:
来实现不发包修改应用icon的展示。
可以通过[[UIApplication sharedApplication] alternateIconName]
来获取当前显示的icon名称,为空可认为显示的是默认icon。
实现
-
- 项目icon配置
在xcode13
以上,不需要
手动去info.plist
里面添加配置iOS files(iOS 5)
选项。编译后会在ipa里面自动生成该选项。
可在asset
上配置多套AppIcon
, 在图片资源里添加需要内置的icon,选择全部size
- 项目icon配置
-
需要在
build setting
里配置Include All App Icon Assets
为YES,debug/release都需要设为yes,没注意只配置了debug模式,导致生产失效了。。。。。。。。。。。 -
-
- 代码设置
//使用的时候需要注意在 10.3以上才支持
if (@available(iOS 10.3, *)) {
if ([UIApplication sharedApplication].supportsAlternateIcons) {
[self yxHandleIconChange:repModel.festivalIcon];
}
}
// 更换过之后不再做处理,再次更换才处理。
-(void)yxHandleIconChange:(NSString *)iconFlag {
NSString *iconName = nil;
if ([iconFlag isEqual:@"1"]) {
iconName = @"AppIconYuandan";
} else if ([iconFlag isEqual:@"2"]){
iconName = @"AppIconNewYear";
} else { // 默认
iconName = nil;
}
[self yxHandleIconChangeWithName:iconName];
}
- (void)yxHandleIconChangeWithName:(NSString *)iconName {
NSString *alternateIconName = [[UIApplication sharedApplication] alternateIconName];
if ((!alternateIconName) && (!iconName)) { // 都是nil不处理,已是默认icon了
}else {
if ([alternateIconName isEqual:iconName]) {
} else {
[[UIApplication sharedApplication] setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) {
DEBUGLog(@"替换icon:iconNameStr:%@ ===:%@", iconName, error);
}];
}
}
}
-
- 系统弹出提示更换icon的问题处理。
// 通过运行时机制,在弹出提示时静默处理
#import "UIViewController+HidenChangeAlert.h"
#import <objc/runtime.h>
@interface UIViewController (HidenChangeAlert)
@end
@implementation UIViewController (HidenChangeAlert)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method presentM = class_getInstanceMethod(self.class, @selector(presentViewController:animated:completion:));
Method presentSwizzlingM = class_getInstanceMethod(self.class, @selector(xz_presentViewController:animated:completion:));
method_exchangeImplementations(presentM, presentSwizzlingM);
});
}
- (void)xz_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
if ([viewControllerToPresent isKindOfClass:[UIAlertController class]]) {
// DEBUGLog(@"title runtime : %@",((UIAlertController *)viewControllerToPresent).title);
// DEBUGLog(@"message runtime : %@",((UIAlertController *)viewControllerToPresent).message);
UIAlertController *alertController = (UIAlertController *)viewControllerToPresent;
if (alertController.title == nil && alertController.message == nil) {
return;
} else {
[self xz_presentViewController:viewControllerToPresent animated:flag completion:completion];
return;
}
}
[self xz_presentViewController:viewControllerToPresent animated:flag completion:completion];
}
@end
总结
- Ios10.3以上才支持
- xcode13 以上快捷直观设置待替换icon
- 代码控制处理。
- 弹出提示静默处理