日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS - 让APP动态更改icon

發(fā)布時間:2023/12/20 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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);}}]; }@end

2.

#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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。