馬上6.18,有沒有發(fā)現(xiàn)京東淘寶的logo都在不經(jīng)意間就替換成618主題的了呢,接下來(lái)我們來(lái)一步步仿照,實(shí)現(xiàn)logo無(wú)感知修改功能吧
1.進(jìn)入項(xiàng)目的Assets.xcassets,點(diǎn)擊加號(hào)添加需要替換的備用logo,好在更新了Xcode14,僅需一張1024尺寸的他就能生成其余對(duì)應(yīng)的圖標(biāo)(不得不說(shuō)省事多了??),接下來(lái)可以給你的圖標(biāo)取名(命名隨意,主要是區(qū)分開)
2.進(jìn)入項(xiàng)目的TARGEST 選擇Bundle Settings 搜索 Include All App Icon Assets 將bool值改成YES

現(xiàn)在實(shí)現(xiàn)[[UIApplication sharedApplication]setAlternateIconName:imageStr completionHandler:nil];即可實(shí)現(xiàn)替換Logo,但是替換完成后系統(tǒng)會(huì)有一個(gè)彈窗提示,因?yàn)槲覀円龅綗o(wú)感知,接下來(lái)則需要利用runtime來(lái)把presentViewController方法替換,demo如下:
先創(chuàng)建一個(gè)分類

.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:@"的圖標(biāo)"] &&
? ? ? ? ? ? ? ? [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.接下來(lái)是實(shí)現(xiàn)部分
引入頭文件?
#import "UIApplication+JSIconChange.h"
調(diào)用方法
? ? [[UIApplication sharedApplication]updateAppIconWithName:@"你需要替換的LOGO名稱"];
接下來(lái)就可以實(shí)現(xiàn)該功能了,小伙伴們快去嘗試吧!