iOS - 让APP动态更改icon
生活随笔
收集整理的這篇文章主要介紹了
iOS - 让APP动态更改icon
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
話不多說,直接進主題,注意幾點。
1. 首先我們要知道更改APP的icon, 需要添加Info.plist 中的?Icon files (iOS 5) ->?CFBundleAlternateIcons , 給CFBundleAlternateIcons 增加一些內(nèi)容,這些內(nèi)容就是你 icon 的圖片名字。
2. 其次我們要知道,如果不額外處理,那么我們在更改icon 的時候系統(tǒng)會彈出提示框,告訴用戶icon 已經(jīng)更改, 這樣的用戶體驗是不好的,所以一般都會增加擴展后靜默更改icon。
3. 這種動態(tài)更改是需要提前為 圖片 和 Info.plist 做好準備的,當然如果有特殊需求可以想想辦法通過額外的邏輯完成。
4. 我們核心對象是‘UIAlternateApplicationIcons’ ,我會在后面上傳一段Gif 和 三張截圖,為你介紹這個功能。
?
下面是相關(guān)使用和擴展的代碼 - 我知道有的小伙伴比較懶,直接復(fù)制就好
1.?
#import "ViewController.h"@interface ViewController () @property (nonatomic, copy)NSString *str_IconName;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];_str_IconName = @"F";// Do any additional setup after loading the view. }- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{if (![[UIApplication sharedApplication] supportsAlternateIcons]) {return;}if ([_str_IconName isEqualToString:@"F"]) {_str_IconName = @"S";}else{_str_IconName = @"F";}[[UIApplication sharedApplication] setAlternateIconName:_str_IconName completionHandler:^(NSError * _Nullable error) {if (error) {NSLog(@"更換app圖標發(fā)生錯誤了 : %@",error);}}]; }@end2.
#import "UIViewController+Present.h" #import <objc/runtime.h>@implementation UIViewController (Present)+ (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:));// 交換方法實現(xiàn)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 中title 和message 都沒有信息 那么返回空,這樣就不會彈框,當然你可以判斷你需要的。UIAlertController *alertController = (UIAlertController *)viewControllerToPresent;if (alertController.title == nil && alertController.message == nil) {NSLog(@"iconName = %@",[[UIApplication sharedApplication] alternateIconName]);return;} else {[self dy_presentViewController:viewControllerToPresent animated:flag completion:completion];return;}}[self dy_presentViewController:viewControllerToPresent animated:flag completion:completion]; }@end以上就是讓APP動態(tài)更改icon 的相關(guān)內(nèi)容。
感謝學習,學以致用更感謝~?
?
?
總結(jié)
以上是生活随笔為你收集整理的iOS - 让APP动态更改icon的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 贪心算法-2.找钱问题
- 下一篇: 无线路由器使用方法,教你怎么设置无线路由