Swift動(dòng)態(tài)修改Icon,消除系統(tǒng)彈窗

背景

電商項(xiàng)目總喜歡在特定節(jié)日展示不同的icon圖標(biāo),但是為了修改圖標(biāo)而更新版本的話,沒那個(gè)必要。
這里介紹一種不需要通過更新版本就可以動(dòng)態(tài)修改APP圖標(biāo)的方法。

??注意:該方法在iOS10.3及以上有效

實(shí)現(xiàn)

  • 1 導(dǎo)入待替換的新圖片,放到項(xiàng)目工程新文件夾中;
  • 2 配置 Info.plist 文件:
<key>CFBundleIcons</key>
<dict>
    <key>CFBundleAlternateIcons</key>
    <dict>
        <key>icon1</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string>icon1</string>
            </array>
            <key>UIPrerenderedIcon</key>
            <false/>
        </dict>
        <key>icon2</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string>icon2</string>
            </array>
            <key>UIPrerenderedIcon</key>
            <false/>
        </dict>
    </dict>
    <key>CFBundlePrimaryIcon</key>
    <dict>
        <key>CFBundleIconFiles</key>
    <array>
        <string>AppIcon60x60</string>
    </array>
    </dict>
</dict>
  • 3 通過代碼替換
        if #available(iOS 10.3, *) {
            if UIApplication.shared.supportsAlternateIcons {
                print("you can change this app's icon")
            }else {
                print("you cannot change this app's icon")
                return
            }
            
            if let name = UIApplication.shared.alternateIconName {
                // CHANGE TO PRIMARY ICON  恢復(fù)默認(rèn) icon
                UIApplication.shared.setAlternateIconName(nil) { (err:Error?) in
                    print("set icon error:\(String(describing: err))")
                }
                print("the alternate icon's name is \(name)")
            }else {
                // CHANGE TO ALTERNATE ICON 指定icon圖標(biāo)
                UIApplication.shared.setAlternateIconName("icon1") { (err:Error?) in
                    print("set icon error:\(String(describing: err))")
                }
            }
        }
  • 4 去掉更換圖標(biāo)時(shí)的彈框
    更換圖標(biāo)時(shí)會(huì)出現(xiàn)系統(tǒng)彈框,可以使用Runtime來隱藏彈框,這樣方便在節(jié)日時(shí)候程序自動(dòng) 無感 更改APP 的icon
    具體代碼如下:
extension UIViewController {

  //通過運(yùn)行時(shí)替換系統(tǒng)的present方法

  public class func nkReplaceSystemPresent(){

    let systemSelector = #selector(UIViewController.present(_:animated:completion:))

    let nkSelector = #selector(UIViewController.newPesent(_:animated:completion:))



    let systemMethod = class_getInstanceMethod(self, systemSelector)

    let nkNewMethod = class_getInstanceMethod(self, nkSelector)
      method_exchangeImplementations(systemMethod!, nkNewMethod!)

  }



  @objc public func newPesent(_ vcToPresent:UIViewController, animated flag:Bool, completion: (() ->Void)? = nil) {

    if vcToPresent.isKind(of:UIAlertController.self) {

      let alertController = vcToPresent as? UIAlertController

      if alertController?.title==nil && alertController?.message==nil {
        return
      }
    }

    self.newPesent(vcToPresent, animated: flag)

  }

}
 

然后在滿足需求的控制器中調(diào)用即可。
方法如下:

    override func viewDidLoad() {
        super.viewDidLoad()
        DH_MyViewController.nkReplaceSystemPresent()
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 動(dòng)態(tài)更換app的圖標(biāo),就是在不重新安裝app的情況下,可以動(dòng)態(tài)更黃當(dāng)前的icon圖標(biāo)。該方法只能在系統(tǒng)版本iOS1...
    光之鹽汽水閱讀 11,479評(píng)論 1 13
  • 該功能應(yīng)用的場(chǎng)景 1、白天/夜間模式切換,在切換App主色調(diào)同時(shí)切換App圖標(biāo)。 2、各類皮膚主題(淘寶就可換膚)...
    Dayon閱讀 2,409評(píng)論 0 3
  • 背景 項(xiàng)目開發(fā)過程中,可能會(huì)有更換項(xiàng)目圖標(biāo)的需求,比如一些電商的應(yīng)用,在雙11,618,新年等節(jié)日的時(shí)候需要顯示特...
    yuebiubiu閱讀 1,850評(píng)論 1 6
  • 首先需要注意的是,動(dòng)態(tài)更換圖標(biāo)需要在iOS10.3之后才可以使用 第一步、需要在info.plist中配置圖標(biāo)的一...
    i_蘇閱讀 1,332評(píng)論 2 2
  • 用兩張圖告訴你,為什么你的 App 會(huì)卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 14,108評(píng)論 2 59

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