一、准备好icon图标 然后放到项目中 如图所示
注意:1、图片肯定不要放到 Assets.xcassets 内里
2、icon图片的名称可以随意取
3、icon图片可以为一张 也可以为多少张(由于适配 以是我这里是多张 按照苹果的尺寸来计划的图片)
二、设置 Info.plist
如上图
末了照如图设置
此时设置完成 ,
注意:1、Icon files (iOS 5) 这个键 最好用我上面的方式来添加 由于内里有默认的两个键值 假如本身创建则是空的(固然本身创建也可以 但是字母肯定不要写错)
2、紧张的键值:CFBundleIconFiles(肯定不要错)
UIPrerenderedIcon(肯定不要错)
3、图片对应的名称 肯定要与项目中的图片名称同等
三、代码设置
- (void)changeAppIconWithName NSString *)iconName { if (![[UIApplication sharedApplication] supportsAlternateIcons]) { return; } if ([iconName isEqualToString ""]) { iconName = nil; } [[UIApplication sharedApplication] setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) { if (error) { NSLog(@"更换app图标发生错误了 : %@",error); }else{ NSLog(@"更换乐成"); } }];}这里的 iconName 就是你 Info.plist 内里设置的 name 如图
注意:名称肯定要对应
到此 项目设置完成
但是 此时你会发现 每次更换图标就会弹出一个提示框 更换图标用户就会有感知,
四、利用runtime的方法交换 来拦截弹框事故,实现方法转换,从而去掉弹框 做到无感知
详细做法,创建一个 UIViewController的 Category(分类)
创建方式如图
然后详细代码:如下
//// UIViewController+Category.m// ChangeIcon// 这个目标是拦截更换图标时的弹框// Created by 冯闯 on 2022/6/23.//#import "UIViewController+Category.h"#import <objc/runtime.h>@implementation UIViewController (Category)+ (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(dismissAlertViewController:animated:completion ); //runtime方法交换,通过拦截弹框事故,实现方法转换,从而去掉弹框 method_exchangeImplementations(presentM, presentSwizzlingM); });}- (void)dismissAlertViewController UIViewController *)viewControllerToPresent animated BOOL)animated completion void (^)(void))completion { if ([viewControllerToPresent isKindOfClass:[UIAlertController class]]) { UIAlertController *alertController = (UIAlertController *)viewControllerToPresent; if (alertController.title == nil && alertController.message == nil) { return; } } [self dismissAlertViewController:viewControllerToPresent animated:animated completion:completion];}@end到此 所有设置已经完成
注意:发起利用真机测试,模仿器可能会不停报错 更换图标不乐成
我这边利用模仿器报如下错误(但是真zh)
利用模仿器 如下
The requested operation couldn’t be completed because the feature is not supported.demo 所在 https://gitee.com/fc1024/change-icon.git |