热门搜索 :
考研考公
您的当前位置:首页正文

替换AppIcon(iOS10.3)

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

准备工作

首先拖入需要替换的Icon


image.png

然后修改info.plist


image.png
<key>CFBundleIcons</key>
    <dict>
        <key>CFBundleAlternateIcons</key>
        <dict>
            <key>晴</key>
            <dict>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>晴</string>
                </array>
                <key>UIPrerenderedIcon</key>
                <false/>
            </dict>
            <key>多云</key>
            <dict>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>多云</string>
                </array>
                <key>UIPrerenderedIcon</key>
                <false/>
            </dict>
            <key>小雨</key>
            <dict>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>小雨</string>
                </array>
                <key>UIPrerenderedIcon</key>
                <false/>
            </dict>
            <key>大雨</key>
            <dict>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>大雨</string>
                </array>
                <key>UIPrerenderedIcon</key>
                <false/>
            </dict>
            <key>雪</key>
            <dict>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>雪</string>
                </array>
                <key>UIPrerenderedIcon</key>
                <false/>
            </dict>
        </dict>
        <key>CFBundlePrimaryIcon</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string></string>
            </array>
            <key>UIPrerenderedIcon</key>
            <false/>
        </dict>
        <key>UINewsstandIcon</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string></string>
            </array>
            <key>UINewsstandBindingType</key>
            <string>UINewsstandBindingTypeMagazine</string>
            <key>UINewsstandBindingEdge</key>
            <string>UINewsstandBindingEdgeLeft</string>
        </dict>
    </dict>

核心代码

    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];
}

效果图

弹窗


696588-03a2cedefec2fd40.gif

不弹窗


696588-09f00ce73de73194.gif

Demo地址:

Top