iOS-OC-应用Icon内置多套切换展示

  • 应用说明

关于节日时,app安装在手机桌面展示的icon变化处理,
iOS10.3以后可通过设置setAlternateIconName:来实现不发包修改应用icon的展示。
可以通过[[UIApplication sharedApplication] alternateIconName]来获取当前显示的icon名称,为空可认为显示的是默认icon。

1. 设置icon方式

实现

    1. 项目icon配置
      xcode13以上,不需要手动去info.plist里面添加配置iOS files(iOS 5)选项。编译后会在ipa里面自动生成该选项。
      通过xcode13设置多套icon后,ipa里的info.plis文件会自动生成icon files(iOS 5)选项
      可在asset上配置多套AppIcon, 在图片资源里添加需要内置的icon,选择全部size

在图片资源里添加内置的icon设置

  • 需要在build setting里配置Include All App Icon Assets为YES,debug/release都需要设为yes,没注意只配置了debug模式,导致生产失效了。。。。。。。。。。。

  • 需要在设置里配置All App Incon Assets为yes

    1. 代码设置
//使用的时候需要注意在 10.3以上才支持
 if (@available(iOS 10.3, *)) {
                        if ([UIApplication sharedApplication].supportsAlternateIcons) {
                            [self yxHandleIconChange:repModel.festivalIcon];
                         }
                    }
// 更换过之后不再做处理,再次更换才处理。
-(void)yxHandleIconChange:(NSString *)iconFlag {
    NSString *iconName = nil;
    if ([iconFlag isEqual:@"1"]) { 
        iconName = @"AppIconYuandan";
    } else if ([iconFlag isEqual:@"2"]){ 
        iconName = @"AppIconNewYear";
    } else { // 默认
        iconName = nil;
    }
    [self yxHandleIconChangeWithName:iconName];
}

- (void)yxHandleIconChangeWithName:(NSString *)iconName {
    NSString *alternateIconName = [[UIApplication sharedApplication] alternateIconName];
    if ((!alternateIconName) && (!iconName)) { // 都是nil不处理,已是默认icon了
    }else {
        if ([alternateIconName isEqual:iconName]) {
            
        } else {
            [[UIApplication sharedApplication] setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) {
                DEBUGLog(@"替换icon:iconNameStr:%@ ===:%@", iconName, error);
            }];
        }
    }
}

    1. 系统弹出提示更换icon的问题处理。
// 通过运行时机制,在弹出提示时静默处理
#import "UIViewController+HidenChangeAlert.h"
#import <objc/runtime.h>

@interface UIViewController (HidenChangeAlert)

@end

@implementation UIViewController (HidenChangeAlert)

+ (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(xz_presentViewController:animated:completion:));
        method_exchangeImplementations(presentM, presentSwizzlingM);
    });
}

- (void)xz_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
    if ([viewControllerToPresent isKindOfClass:[UIAlertController class]]) {
//        DEBUGLog(@"title runtime : %@",((UIAlertController *)viewControllerToPresent).title);
//        DEBUGLog(@"message runtime : %@",((UIAlertController *)viewControllerToPresent).message);
        UIAlertController *alertController = (UIAlertController *)viewControllerToPresent;
        if (alertController.title == nil && alertController.message == nil) {
            return;
        } else {
            [self xz_presentViewController:viewControllerToPresent animated:flag completion:completion];
            return;
        }
    }
    [self xz_presentViewController:viewControllerToPresent animated:flag completion:completion];
}

@end

总结

  1. Ios10.3以上才支持
  2. xcode13 以上快捷直观设置待替换icon
  3. 代码控制处理。
  4. 弹出提示静默处理
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值