美文网首页iOS
实现iOS应用的icon动态替换

实现iOS应用的icon动态替换

作者: Areslee | 来源:发表于2019-10-08 11:37 被阅读0次

    先看效果


    iconchange.gif

    注意三个地方就行了
    1.将需要替换的备选icon图片放在工程文件夹下,不要放在Assets.xcassets文件夹下。
    2.在info.plist里面配置相关键值对,如下图所示


    icon.jpg

    3.在你想要添加切换icon功能的控制器下,添加如下关键代码即可:

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        UIButton *changeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        changeBtn.frame = CGRectMake(100, 100, 100, 40);
        [changeBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [changeBtn setTitle:@"替换icon" forState:UIControlStateNormal];
        [changeBtn setTitle:@"还原icon" forState:UIControlStateSelected];
        [changeBtn addTarget:self action:@selector(changeClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:changeBtn];
    }
    
    
    -(void)changeClick:(UIButton *)sender{
        sender.selected = !sender.isSelected;
        if (sender.selected == YES) {
            [[UIApplication sharedApplication] setAlternateIconName:@"iconbear" completionHandler:^(NSError * _Nullable error) {
                if (error == nil) {
                    NSLog(@"替换成功");
                }else{
                    NSLog(@"替换失败");
                }
            }];
        }else{
            [[UIApplication sharedApplication] setAlternateIconName:nil completionHandler:^(NSError * _Nullable error) {
                if (error == nil) {
                    NSLog(@"还原成功");
                }else{
                    NSLog(@"还原失败");
                }
            }];
        }
    }
    
    

    根据最新的苹果app审核指南

    4.6 备选 App 图标
    App 可以使用自定图标以传达特定信息 (例如表达对某个运动团队的喜爱),前提是每次更改都由用户发起,并且 app 中应包含恢复至原始图标的设置。所有图标变体必须与 app 的内容相关,并且更改内容在所有系统资源之间应保持一致,以便“设置”和“通知”等位置中显示的图标与新的 Springboard 图标相吻合。这项功能不可用于动态、自动或连续性更改,例如用于反映最新天气信息和日历通知等。

    上述提到需要包含一个恢复至原始图标的设置,那么我们就需要按照要求添加一个一键恢复原始图标的功能即可。

    demo地址:https://github.com/BHAreslee/changeAppIconDemo

    本人微信公众号:放心安慰剂

    image

    相关文章

      网友评论

        本文标题:实现iOS应用的icon动态替换

        本文链接:https://www.haomeiwen.com/subject/aocoectx.html