配置info.plist
<!-- 开始-->
<key>CFBundleIcons</key>
<dict>
<key>CFBundleAlternateIcons</key>
<dict>
<key>春</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<!-- icon图片名字-->
<string>春</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
<key>夏</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<!-- icon图片名字-->
<string>夏</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
<key>秋</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<!-- icon图片名字-->
<string>秋</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
<key>冬</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<!-- icon图片名字-->
<string>冬</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
</dict>
<key>UINewsstandIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string></string>
</array>
<key>UINewsstandBindingEdge</key>
<string>UINewsstandBindingEdgeLeft</string>
<key>UINewsstandBindingType</key>
<string>UINewsstandBindingTypeMagazine</string>
</dict>
</dict>
<!-- 结束-->
配置icon图片建个文件夹 直接放进去不能用Assets.xcassets
上代码
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self changeAppIconWithName:@"春"];
}
- (void)changeAppIconWithName:(NSString *)iconName
{
if (![[UIApplication sharedApplication] supportsAlternateIcons])
{
return;
}
if ([iconName length] <= 0)
{
return;
}
[[UIApplication sharedApplication] setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) {
if (error)
{
NSLog(@"更换icon发生错误=%@",error);
}
else
{
NSLog(@"最后的提示框只是提示 确定按钮没有意义 可以去掉");
}
}];
}
静默替换的处理
#import "UIViewController+HidenAlertViewController.h"
#import <objc/message.h>
#import <objc/runtime.h>
@implementation UIViewController (HidenAlertViewController)
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method present = class_getInstanceMethod(self.class, @selector(presentViewController:animated:completion:));
Method presentNew = class_getInstanceMethod(self.class, @selector(new_presentViewController:animated:completion:));
method_exchangeImplementations(present, presentNew);
});
}
- (void)new_presentViewController:(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 new_presentViewController:viewControllerToPresent animated:animated completion:completion];
}
@end
++++++++++++++++++++++官网翻译+++++++++++++++++++++++++++
宣言
- (void)setAlternateIconName:(NSString *)alternateIconName
completionHandler:(void (^)(NSError *error))completionHandler;
参数
alternateIconName
备用图标的名称,在应用程序文件的密钥中声明。指定是否要显示应用程序的主要图标,使用该密钥声明该图标。这两个键都是应用程序文件中键的子条目。CFBundleAlternateIcons
Info.plist
nil
CFBundlePrimaryIcon
CFBundleIcons
Info.plist
completionHandler
要使用结果执行的处理程序。尝试更改应用程序的图标后,系统会通过调用您的处理程序来报告结果。(处理程序在UIKit提供的队列上执行,而不一定在应用程序的主队列上执行。)处理程序没有返回值并采用以下参数:
错误
成功时,此参数的值为nil
。如果发生错误,则此参数包含指示发生了什么的错误对象,并且该属性的值保持不变。alternateIconName
讨论
使用此方法可将应用程序的图标更改为其主要图标或其备用图标之一。只有在属性值为的情况下,才能更改图标。supportsAlternateIcons
YES
您必须使用应用程序文件的密钥声明应用程序的主要和备用图标。有关如何为应用程序配置备用图标的信息,请参阅“ 信息属性列表键参考”中的键说明。CFBundleIcons
Info.plist
CFBundleIcons
也可以看看
管理应用程序的图标
当前设置为Springboard中应用程序图标徽章的数字。
一个布尔值,指示是否允许应用程序更改其图标。
应用程序显示的图标名称。