话不多说,直接进主题,注意几点。
1. 首先我们要知道更改APP的icon, 需要添加Info.plist 中的 Icon files (iOS 5) -> CFBundleAlternateIcons , 给CFBundleAlternateIcons 增加一些内容,这些内容就是你 icon 的图片名字。
2. 其次我们要知道,如果不额外处理,那么我们在更改icon 的时候系统会弹出提示框,告诉用户icon 已经更改, 这样的用户体验是不好的,所以一般都会增加扩展后静默更改icon。
3. 这种动态更改是需要提前为 图片 和 Info.plist 做好准备的,当然如果有特殊需求可以想想办法通过额外的逻辑完成。
4. 我们核心对象是‘UIAlternateApplicationIcons’ ,我会在后面上传一段Gif 和 三张截图,为你介绍这个功能。
下面是相关使用和扩展的代码 - 我知道有的小伙伴比较懒,直接复制就好
1.
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, copy)NSString *str_IconName;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_str_IconName = @"F";
// Do any additional setup after loading the view.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
if (![[UIApplication sharedApplication] supportsAlternateIcons]) {
return;
}
if ([_str_IconName isEqualToString:@"F"]) {
_str_IconName = @"S";
}else{
_str_IconName = @"F";
}
[[UIApplication sharedApplication] setAlternateIconName:_str_IconName completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"更换app图标发生错误了 : %@",error);
}
}];
}
@end
2.
#import "UIViewController+Present.h"
#import <objc/runtime.h>
@implementation UIViewController (Present)
+ (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(dy_presentViewController:animated:completion:));
// 交换方法实现
method_exchangeImplementations(presentM, presentSwizzlingM);
});
}
- (void)dy_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
if ([viewControllerToPresent isKindOfClass:[UIAlertController class]]) {
// NSLog(@"title : %@",((UIAlertController *)viewControllerToPresent).title);
// NSLog(@"message : %@",((UIAlertController *)viewControllerToPresent).message);
//这里只是判断了 如果UIAlertController 中title 和message 都没有信息 那么返回空,这样就不会弹框,当然你可以判断你需要的。
UIAlertController *alertController = (UIAlertController *)viewControllerToPresent;
if (alertController.title == nil && alertController.message == nil) {
NSLog(@"iconName = %@",[[UIApplication sharedApplication] alternateIconName]);
return;
} else {
[self dy_presentViewController:viewControllerToPresent animated:flag completion:completion];
return;
}
}
[self dy_presentViewController:viewControllerToPresent animated:flag completion:completion];
}
@end
以上就是让APP动态更改icon 的相关内容。
感谢学习,学以致用更感谢~