iOS开发笔记 -- 动态切换APP的logo

1、618大促,看到天猫与京东的logo 也相应改变,所以查找资料 探究其实现方式。
2、实现的过程并不复杂,在此做个笔记,在今后的项目中可能会用到。
3、本篇笔记 demo

1、先看一下实现的效果。

这里写图片描述

2、苹果在10.3开放了一个新的API,就是更换APP的Icon图标。alternateIconName: logo的名称,为nil时 则为 默认的。

- (void)setAlternateIconName:(nullable NSString *)alternateIconName completionHandler:(nullable void (^)(NSError *_Nullable error))completionHandler NS_EXTENSION_UNAVAILABLE("Extensions may not have alternate icons") API_AVAILABLE(ios(10.3), tvos(10.2));

3、配置 Info.plist: 选中 Info.plist 文件,添加 Icon files (iOS 5),Primary Icon 是默认的图标,我们还需在其里面添加 CFBundleAlternateIcons 字段,配置如下所示,logo1 就是添加在目录里面logo的图片名称。

这里写图片描述

4、切换logo时 系统会弹出一个默认的弹出框,在实际的项目中,我们可以利用runtime来替换弹出框的方法:

// 利用runtime来替换展现弹出框的方法
- (void)runtimeReplaceAlert
{
    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(ox_presentViewController:animated:completion:));
        // 交换方法实现
        method_exchangeImplementations(presentM, presentSwizzlingM);
    });
}

- (void)ox_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {

    if ([viewControllerToPresent isKindOfClass:[UIAlertController class]]) {
        // 换图标时的提示框的title和message都是nil,由此可特殊处理
        UIAlertController *alertController = (UIAlertController *)viewControllerToPresent;
        if (alertController.title == nil && alertController.message == nil) { // 是换图标的提示
            return;
        } else {// 其他提示还是正常处理
            [self ox_presentViewController:viewControllerToPresent animated:flag completion:completion];
            return;
        }
    }

    [self ox_presentViewController:viewControllerToPresent animated:flag completion:completion];
}

5、实现效果如下:

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_31748121/article/details/80596567