动态改变APP图标
一、iOS動態(tài)更換App圖標(一):基礎使用
該功能應用的場景
1、白天/夜間模式切換,在切換App主色調(diào)同時切換App圖標。
2、各類皮膚主題(淘寶就可換膚),附帶App圖標一塊更換。
3、利用App圖標表達某種特定功能,如Demo中的,提示當前天氣。
4、圖標促銷提示,如淘寶京東特定節(jié)日:11.11、6.18,提前更換App圖標。
當然該功能(API)當前只支持iOS10.3以上的系統(tǒng),所以只能當做一項附加功能來進行使用。下面將詳細講解下如何使用代碼來實現(xiàn)此功能。
API方法
@interface UIApplication (UIAlternateApplicationIcons) // 如果為NO,表示當前進程不支持替換圖標 @property (readonly, nonatomic) BOOL supportsAlternateIcons NS_EXTENSION_UNAVAILABLE("Extensions may not have alternate icons") API_AVAILABLE(ios(10.3), tvos(10.2)); // 傳入nil代表使用主圖標. 完成后的操作將會在任意的后臺隊列中異步執(zhí)行; 如果需要更改UI,請確保在主隊列中執(zhí)行. - (void)setAlternateIconName:(nullable NSString *)alternateIconName completionHandler:(nullable void (^)(NSError *_Nullable error))completionHandler NS_EXTENSION_UNAVAILABLE("Extensions may not have alternate icons") API_AVAILABLE(ios(10.3), tvos(10.2)); // 如果alternateIconName為nil,則代表當前使用的是主圖標. @property (nullable, readonly, nonatomic) NSString *alternateIconName NS_EXTENSION_UNAVAILABLE("Extensions may not have alternate icons") API_AVAILABLE(ios(10.3), tvos(10.2)); @end - (void)setAppIconWithName:(NSString *)iconName {if (![[UIApplication sharedApplication] supportsAlternateIcons]) {return;}if ([iconName isEqualToString:@""]) {iconName = nil;}[[UIApplication sharedApplication] setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) {if (error) {NSLog(@"更換app圖標發(fā)生錯誤了 : %@",error);}}]; }?
配置文件(Info.plist)
當然也要拖入對應的App圖標:
不過這里我們好像還少配置了App主圖標,也就是正常情況下我們的圖標。按照文檔所說,我們需要在CFBundleIcons里面配置CFBundlePrimaryIcon這個主圖標對應的內(nèi)容,但是實際上,我們還是按照老方法,在Assets.xcassets中配置AppIcon,對應尺寸填上對應圖片即可。為什么這樣子就可以配置主圖標呢?讓我們來看看某知名電商的ipa(在AppStore上下載的包)內(nèi)的Info.plist(位于Payload/XXXXXX/Info.plist):
攔截彈框
既然知道了彈框是UIAlertController,那么我們自然而然想到,該彈框是由ViewController通過presentViewController:animated:completion:方法彈出。那么我們就可以通過Method swizzling?hook該彈框,不讓其進行彈出即可:
#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:));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 *alertController = (UIAlertController *)viewControllerToPresent;if (alertController.title == nil && alertController.message == nil) {return;} else {[self dy_presentViewController:viewControllerToPresent animated:flag completion:completion];return;}}[self dy_presentViewController:viewControllerToPresent animated:flag completion:completion]; }@end或者
#import "ViewController.h" #import <objc/runtime.h>// 利用runtime來替換展現(xiàn)彈出框的方法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(ox_presentViewController:animated:completion:));// 交換方法實現(xiàn) method_exchangeImplementations(presentM, presentSwizzlingM);});// 自己的替換展示彈出框的方法 - (void)ox_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);// 換圖標時的提示框的title和message都是nil,由此可特殊處理UIAlertController *alertController = (UIAlertController *)viewControllerToPresent;if (alertController.title == nil && alertController.message == nil) {// 是換圖標的提示return;} else {// 其他提示還是正常處理 [self ox_presentViewController:viewControllerToPresent animated:flag completion:completion];return;}}[self ox_presentViewController:viewControllerToPresent animated:flag completion:completion]; }?
?
這段代碼交換了UIViewController的presentViewController:animated:completion:方法。通過打印UIAlertController的特征,我們可以發(fā)現(xiàn),更換App圖標時的彈框是沒有title與message的,但是我們一般使用的UIAlertController都是帶title、message的,畢竟不會彈個空白的框給用戶玩。
所以該方法中通過判斷title與message來捕捉更換App圖標時的彈框,并直接return即可。
《iOS動態(tài)更換App圖標(三):動態(tài)下載App圖標進行更換》短期內(nèi)應該無法實現(xiàn)
?
轉(zhuǎn)載于:https://www.cnblogs.com/fengmin/p/6841818.html
總結(jié)
- 上一篇: SRS学习笔记7-SrsHttpServ
- 下一篇: cocos2d-x 3.0 常见问题及解