1. 导入图片.图片放在Assets.xcassets 里不好使.放工程里
2.配置.plist如下
3.代码:
- (void)change
{
if (![[UIApplication sharedApplication] supportsAlternateIcons]) {
NSLog(@"retrun");
return;
}
NSString *iconName = [NSString stringWithFormat:@"%d",self.num];
NSLog(@"%@",iconName);
[[UIApplication sharedApplication] setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"更换app图标发生错误了 : %@",error);
} else {
NSLog(@"更换app-success");
}
}];
self.num += 1;
if (self.num == 6) {
self.num = 3;
}
}
4.更换的时候,系统方法会有弹框.重新系统方法代码如下:
//
// UIViewController+Present.h
// IconTest
//
// Created by 司文 on 2019/8/7.
// Copyright © 2019 司文. All rights reserved.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIViewController (Present)
@end
NS_ASSUME_NONNULL_END
//
// UIViewController+Present.m
// IconTest
//
// Created by 司文 on 2019/8/7.
// Copyright © 2019 司文. All rights reserved.
//
#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]);
// NSLog(@"IsListening: %@\nCallStacks:\n%@", [[UIApplication sharedApplication] alternateIconName], NSThread.callStackSymbols);
//
// return;
// } else {
// [self dy_presentViewController:viewControllerToPresent animated:flag completion:completion];
// return;
// }
// }
//
// [self dy_presentViewController:viewControllerToPresent animated:flag completion:completion];
}
@end
以上参考.胖胖大神博客:https://blog.csdn.net/siwen1990
以下正逢七夕,用这个做个表白小惊喜喽.如图 可以做成某某某我爱你