替换AppIcon(iOS10.3)

在iOS10.3之前如果想替换APP的图标,必须通过Assets.xcassets添加AppIcon,而且是唯一指定的,不能够修改。
iOS10.3之后,系统提供了修改AppIcon的API,可以通过内置几个不同的icon,然后通过代码设置不同的AppIcon。

准备工作

首先拖入需要替换的Icon


替换AppIcon(iOS10.3)_第1张图片
image.png

然后修改info.plist


替换AppIcon(iOS10.3)_第2张图片
image.png
CFBundleIcons
    
        CFBundleAlternateIcons
        
            
            
                CFBundleIconFiles
                
                    
                
                UIPrerenderedIcon
                
            
            多云
            
                CFBundleIconFiles
                
                    多云
                
                UIPrerenderedIcon
                
            
            小雨
            
                CFBundleIconFiles
                
                    小雨
                
                UIPrerenderedIcon
                
            
            大雨
            
                CFBundleIconFiles
                
                    大雨
                
                UIPrerenderedIcon
                
            
            
            
                CFBundleIconFiles
                
                    
                
                UIPrerenderedIcon
                
            
        
        CFBundlePrimaryIcon
        
            CFBundleIconFiles
            
                
            
            UIPrerenderedIcon
            
        
        UINewsstandIcon
        
            CFBundleIconFiles
            
                
            
            UINewsstandBindingType
            UINewsstandBindingTypeMagazine
            UINewsstandBindingEdge
            UINewsstandBindingEdgeLeft
        
    

核心代码

    NSArray *weathers = @[@"晴", @"多云", @"小雨", @"大雨", @"雪", @""];
    NSString *iconName = weathers[arc4random() % weathers.count];
    
    if (![[UIApplication sharedApplication] supportsAlternateIcons]) {
        return;
    }
    
    if ([iconName isEqualToString:@""]) {
        iconName = nil;
    }
    [[UIApplication sharedApplication] setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) {
        if (error) {
            NSLog(@"更换app图标发生错误了 : %@",error);
        }
    }];

不想弹窗,就用runtime替换调弹窗方法

+ (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 *alertController = (UIAlertController *)viewControllerToPresent;
        if (alertController.title == nil && alertController.message == nil) {
            return;
        } else {
            [self dy_presentViewController:viewControllerToPresent animated:flag completion:completion];
            return;
        }
    }
    
    [self dy_presentViewController:viewControllerToPresent animated:flag completion:completion];
}

效果图

弹窗


替换AppIcon(iOS10.3)_第3张图片
696588-03a2cedefec2fd40.gif

不弹窗


替换AppIcon(iOS10.3)_第4张图片
696588-09f00ce73de73194.gif

Demo地址:https://github.com/vast0608/ChangeIconDemo

你可能感兴趣的:(替换AppIcon(iOS10.3))