前言:前段时间iphone更新了iOS 10.3系统,就关注了一下10.3的一些新增功能。看到有一个是更换图标功能,就感兴趣的上网搜了一下相关信息,KyXu写的这篇对这个功能介绍的简单明了,但是代码是swift版本,我把几句代码改成了OC版本,在此进行分享记录。
Github:
1、设置AppIcon
2、在info.plist添加相关字段
3、添加更换icon入口
UIButton *btn = [[UIButton alloc]init];
[btn setTitleColor:KColorRGB(121, 52, 135) forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
__weak typeof(self) weak = self;
[btn mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(weak.view);
make.size.mas_equalTo(CGSizeMake(100, 20));
}];
实现代码:
- (void)btnClick:(UIButton *)sender{
//判断是否支持更换icon
if ([[UIApplication sharedApplication] supportsAlternateIcons]) {
NSLog(@"you can change icon");
}else{
NSLog(@"you could't change icon");
return;
}
//当前是否已更换icon
if ([[[UIApplication sharedApplication]alternateIconName] isEqualToString:@"blackBgColor"]) {
[[UIApplication sharedApplication]setAlternateIconName:nil completionHandler:^(NSError * _Nullable error) {
NSLog(@"change success!");
}];
}else{
[[UIApplication sharedApplication]setAlternateIconName:@"blackBgColor" completionHandler:^(NSError * _Nullable error) {
NSLog(@"change success!");
}];
}
}