马上6.18,有没有发现京东淘宝的logo都在不经意间就替换成618主题的了呢,接下来我们来一步步仿照,实现logo无感知修改功能吧
1.进入项目的Assets.xcassets,点击加号添加需要替换的备用logo,好在更新了Xcode14,仅需一张1024尺寸的他就能生成其余对应的图标(不得不说省事多了😂),接下来可以给你的图标取名(命名随意,主要是区分开)
2.进入项目的TARGEST 选择Bundle Settings 搜索 Include All App Icon Assets 将bool值改成YES
现在实现[[UIApplication sharedApplication]setAlternateIconName:imageStr completionHandler:nil];即可实现替换Logo,但是替换完成后系统会有一个弹窗提示,因为我们要做到无感知,接下来则需要利用runtime来把presentViewController方法替换,demo如下:
先创建一个分类
.h demo如下
#import
NS_ASSUME_NONNULL_BEGIN
@interfaceUIApplication(JSIconChange)
- (void)updateAppIconWithName:(NSString *)name;
@end
NS_ASSUME_NONNULL_END
.m文件如下
#import "UIApplication+JSIconChange.h"
#import
@interfaceUIViewController(JSIconChange)
@end
@implementationUIViewController(JSIconChange)
+ (void)load{
staticdispatch_once_tonceToken;
dispatch_once(&onceToken, ^{
MethodoriginMethod =class_getInstanceMethod(self.class,@selector(presentViewController:animated:completion:));
MethodswizzMethod =class_getInstanceMethod(self.class,@selector(js_presentViewController:animated:completion:));
method_exchangeImplementations(originMethod, swizzMethod);
});
}
- (void)js_presentViewController:(UIViewController *)viewController
animated:(BOOL)animated
completion:(void(^)(void))completion {
if([viewControllerisKindOfClass:[UIAlertControllerclass]]) {
UIAlertController*alertController = (UIAlertController*)viewController;
if(alertController.actions.count==1&&
alertController.childViewControllers.count==1) {
NSString*actionTitle = [alertController.actionsfirstObject].title;
NSString*message = [[alertController.childViewControllersfirstObject]valueForKeyPath:@"_messageLabel.text"];
if([actionTitleisEqualToString:@"好"] &&
[messagecontainsString:@"的图标"] &&
[messagecontainsString:@"您已更改"]) {
return;
}
if([actionTitleisEqualToString:@"OK"] &&
[messagecontainsString:@"You have changed the icon for"]) {
return;
}
}
}
[selfjs_presentViewController:viewControlleranimated:animatedcompletion:completion];
}
@end
@implementationUIApplication(JSIconChange)
- (void)updateAppIconWithName:(NSString *)name {
NSString *version = [[UIDevice currentDevice] systemVersion];
if ([version compare:@"10.3" options:NSNumericSearch] == NSOrderedAscending) {
return;
}
if (![self supportsAlternateIcons]) {
return;
}
if([nameisEqualToString:@""]) {
name =nil;
}
if ([[self alternateIconName] isEqualToString:name]) {
return;
}
if(![selfalternateIconName] && !name) {
return;
}
[self setAlternateIconName:name completionHandler:^(NSError * _Nullable error) {
if(error) {
NSLog(@"AppLogo替换失败%@",error);
}
}];
}
@end
3.接下来是实现部分
引入头文件
#import "UIApplication+JSIconChange.h"
调用方法
[[UIApplication sharedApplication]updateAppIconWithName:@"你需要替换的LOGO名称"];
接下来就可以实现该功能了,小伙伴们快去尝试吧!