動態改變APP圖標


一、iOS動態更換App圖標(一):基礎使用

該功能應用的場景

1、白天/夜間模式切換,在切換App主色調同時切換App圖標。

2、各類皮膚主題(淘寶就可換膚),附帶App圖標一塊更換。

3、利用App圖標表達某種特定功能,如Demo中的,提示當前天氣。

4、圖標促銷提示,如淘寶京東特定節日:11.11、6.18,提前更換App圖標。

當然該功能(API)當前只支持iOS10.3以上的系統,所以只能當做一項附加功能來進行使用。下面將詳細講解下如何使用代碼來實現此功能。

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代表使用主圖標. 完成后的操作將會在任意的后台隊列中異步執行; 如果需要更改UI,請確保在主隊列中執行.
- (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圖標發生錯誤了 : %@",error);
        }
    }];
}

 

配置文件(Info.plist)

當然也要拖入對應的App圖標:

  不過這里我們好像還少配置了App主圖標,也就是正常情況下我們的圖標。按照文檔所說,我們需要在CFBundleIcons里面配置CFBundlePrimaryIcon這個主圖標對應的內容,但是實際上,我們還是按照老方法,在Assets.xcassets中配置AppIcon,對應尺寸填上對應圖片即可。為什么這樣子就可以配置主圖標呢?讓我們來看看某知名電商的ipa(在AppStore上下載的包)內的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來替換展現彈出框的方法
    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:));
        // 交換方法實現
        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];
}

 

 

這段代碼交換了UIViewControllerpresentViewController:animated:completion:方法。通過打印UIAlertController的特征,我們可以發現,更換App圖標時的彈框是沒有title與message的,但是我們一般使用的UIAlertController都是帶title、message的,畢竟不會彈個空白的框給用戶玩。

所以該方法中通過判斷title與message來捕捉更換App圖標時的彈框,並直接return即可。

《iOS動態更換App圖標(三):動態下載App圖標進行更換》短期內應該無法實現

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM