动态替换iOS app图标

动态替换iOS app图标

步骤:

1、准备两张想要替换的图,不要放到asset中,记着放到文件夹中,如下图:

2、在info.plist文件中添加相关配置:

<key>CFBundleIcons</key>
	<dict>
		<key>CFBundleAlternateIcons</key>
		<dict>
			<key>AppIcon-2</key>
			<dict>
				<key>CFBundleIconFiles</key>
				<array>
					<string>AppIcon-2</string>
				</array>
			</dict>
		</dict>
	</dict>

3、设置代码:

判断节日时间(非必需):

- (BOOL)checkTodayIsFestival {
    NSDate *date = [NSDate new];
    NSString *beginDateString = @"20210208 00:00:00";
    NSString *endDateString = @"20210220 23:59:59";
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyyMMdd HH:mm:ss"];
    NSDate *beginDate = [format dateFromString:beginDateString];
    NSDate *endDate = [format dateFromString:endDateString];
    if ([date isLaterThanDate:beginDate] && [date isEarlierThanDate:endDate]) {
        [NSNotificationCenter.defaultCenter postNotificationName:kChangeSkinNotification object:nil];
        return YES;
    } else {
        return NO;
    }
}

设置图标:

- (void)checkNeedChangeAppIcon {
    if (@available(iOS 10.3, *)) {
        if ([UIApplication sharedApplication].supportsAlternateIcons) {
            NSString *currentIconName = [UIApplication sharedApplication].alternateIconName;
            if ([AppConfig.shared checkTodayIsFestival]) {
                [[UIApplication sharedApplication] setAlternateIconName:@"AppIcon-2" completionHandler:^(NSError * _Nullable error) {
                    if (error) {
                        MMLog(@"change icon error: %@", error.localizedDescription);
                    }
                }];
            } else {
                if (!currentIconName || (currentIconName && [currentIconName isEqualToString:@"AppIcon-2"])) {
                    [[UIApplication sharedApplication] setAlternateIconName:nil completionHandler:^(NSError * _Nullable error) {
                        if (error) {
                            MMLog(@"change icon error: %@", error.localizedDescription);
                        }
                    }];
                }
            }
        }
    }
}

4、把弹框解决掉(非必需):

通过runtime动态处理:

#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(mm_presentViewController:animated:completion:));
        // 交换方法实现
        method_exchangeImplementations(presentM, presentSwizzlingM);
    });
}

- (void)mm_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 *alertController = (UIAlertController *)viewControllerToPresent;
        //都是空的时候认为是换图标的弹框,不弹了
        if (alertController.title == nil && alertController.message == nil && [AppConfig.shared checkTodayIsFestival]) {
            return;
        } else {
            [self mm_presentViewController:viewControllerToPresent animated:flag completion:completion];
            return;
        }
    }
    
    [self mm_presentViewController:viewControllerToPresent animated:flag completion:completion];
}

@end

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s