【iOS 開發】iOS 無彈框更換 app 圖示

從來吃不胖發表於2017-06-08

上篇文章我們詳細檢視了更換App圖示的使用方法,並做了個小Demo。儘管當前我們可以實現動態更換App圖示了,但是每次更換圖示時,蘋果官方給加的“友好提示”對使用者以及開發者都不是那麼“友好“。官方並沒有給出可以不彈框的方法,畢竟App圖示對於蘋果來說是一個很重要的稽核部分,如果任由開發者在上架後不提示使用者而隨意修改圖示,會造成不好的使用者體驗,所以蘋果會在使用此API時彈框告知使用者該App圖示已修改(個人猜想)。

不過今天我們想談談如何突破這個彈框限制(畢竟開發者也不是傻,不會胡亂更換圖示的是不是?)。

本系列文章

  1. iOS動態更換App圖示(一):基礎使用
  2. iOS動態更換App圖示(二):無彈框更換App圖示
  3. iOS動態更換App圖示(三):動態下載App圖示進行更換

Demo演示

【iOS 開發】iOS 無彈框更換 app 圖示
DynamicAppIconDemo2

Demo地址:github.com/maybeisyi/C…

本篇文章對應工程為:DynamicAppIcon(二)

Demo中可以看到,現在我們可以做到不彈框直接修改App圖示。實現該功能後,某些有意思的小功能就能有良好的使用者體驗了:白天/夜間模式切換,在切換App主色調同時切換App圖示。

下面將詳細講解如何"突破"蘋果的限制。

什麼是彈框

讓我們檢視彈框的本質

【iOS 開發】iOS 無彈框更換 app 圖示
Alert

檢視原Demo中的彈框,此彈框與UIAlertController長的倒是挺像的。讓我們來剖析下這個彈框:

【iOS 開發】iOS 無彈框更換 app 圖示
更換圖示的彈框

可以看到彈框就是私有類_UIAlertControllerView,讓我們再對比下系統的UIAlertController:

【iOS 開發】iOS 無彈框更換 app 圖示
UIAlertController彈框

所以更換App時的彈框就是UIAlertController,只不過上面的控制元件不太一樣罷了。(其實我們也能做到在UIAlertController上新增任意控制元件)

攔截彈框

既然知道了彈框是UIAlertController,那麼我們自然而然想到,該彈框是由ViewController通過presentViewController:animated:completion:方法彈出。那麼我們就可以通過Method swizzling hook該彈框,不讓其進行彈出即可:

#import "UIViewController+Present.h"
#import 

@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複製程式碼

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

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

總結

其實關於介面上的東西,利用動態特性沒有什麼是不能做的,蘋果既然公開了動態API,我們就可以通過動態方法去了解甚至改造我們想要的東西,如系統控制元件如何實現等。蘋果的”規範“在應用層面其實是無法阻擋開發者步伐的,當然動態特性也不能夠濫用(如私有方法),畢竟稽核人員才是爸爸。

儘管目前實現了在使用者無感的情況下替換App圖示,但是可替換的圖示還是必須預先放入工程中,並且要在Info.plist內指定。這很大程度上限制了更換圖示的動態性:比如我們某天想要推出一款新主題以及對應的App圖示,但是新的App圖示並沒有預先放入工程的main bundle中,也沒有在Info.plist中進行指定,所以我們在不上架新版本的情況下,無法推出該新App圖示,因此有這了第三篇文章。

第三篇文章:《iOS動態更換App圖示(三):動態下載App圖示進行更換》短期內應該無法實現,具體原因會在文章中說明。

相關文章