iOS 10.3 如何更換 APP 圖標(biāo)

iOS 10.3 開放了更換 app 圖標(biāo)的 API,核心方法是下面這個:

func setAlternateIconName(_ alternateIconName: String?, 
        completionHandler: ((Error?) -> Void)? = nil)

詳情可以參考官方文檔,如果你想要實現(xiàn)更換APP圖標(biāo)的功能,你還需要在 info.plist 添加一些參數(shù)才行,參數(shù)可參考官方注釋

info.plist

把你想替換的圖標(biāo)名稱添加到配置文件中(PS:可以為多個,此處示例只寫兩個)

<dict>
    <key>CFBundleAlternateIcons</key>
    <dict>
        <key>流程_2</key>
        <dict>
            <key>UIPrerenderedIcon</key>
            <false/>
            <key>CFBundleIconFiles</key>
            <array>
                <string>流程_2</string>
            </array>
        </dict>
        <key>流程_1</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string>流程_1</string>
            </array>
            <key>UIPrerenderedIcon</key>
            <false/>
        </dict>
    </dict>
</dict>
</plist>

注意 info.plist 中用到了兩次 圖片名稱

INFO.png

代碼部分

    UIApplication *application = [UIApplication sharedApplication];
    
    if ([application respondsToSelector:@selector(supportsAlternateIcons)]) {
        NSString *name = application.alternateIconName;
        
        if (nil == name) {
            
            if (nil == actionLabel) {
                
                [UIAlertView bk_showAlertViewWithTitle:@"請選擇一個圖標(biāo)" message:nil cancelButtonTitle:@"OK" otherButtonTitles:nil handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                    
                }];
                
            }
            else{
                NSString *imageName = actionLabel.text;//獲取用戶選取的圖標(biāo)名稱(必須和配置文件中的名稱一致),如果只有一個,也可以寫死
                
                //替換圖標(biāo)
                [application setAlternateIconName:imageName completionHandler:^(NSError * _Nullable error) {
                    if (error) {
                        [UIAlertView bk_showAlertViewWithTitle:@"更換圖標(biāo)失敗" message:error.domain cancelButtonTitle:@"OK" otherButtonTitles:nil handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                            
                        }];
                    }
                    
                }];
            }
            
        }
        else{
            //更新為原來的圖標(biāo)
            [application setAlternateIconName:nil completionHandler:^(NSError * _Nullable error) {

                if (error) {
                    [UIAlertView bk_showAlertViewWithTitle:@"更換圖標(biāo)失敗" message:error.domain cancelButtonTitle:@"OK" otherButtonTitles:nil handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                        
                    }];
                }
            }];
        }
        
    }
    else{

        [UIAlertView bk_showAlertViewWithTitle:@"不能更換圖標(biāo)" message:@"此功能僅限10.3及以上系統(tǒng)" cancelButtonTitle:@"OK" otherButtonTitles:nil handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
            
        }];
    }

效果

Simulator Screen Shot 2017年4月1日 11.03.51.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容