溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

iOS中怎么實現(xiàn)動態(tài)更換Icon

發(fā)布時間:2021-06-15 15:35:26 來源:億速云 閱讀:193 作者:Leah 欄目:移動開發(fā)

今天就跟大家聊聊有關(guān)iOS中怎么實現(xiàn)動態(tài)更換Icon,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

iOS 動態(tài)更換Icon

動態(tài)切換 App 的 icon 這個需求,在上一家公司做一款定制 App 時遇到過一次,這次領(lǐng)導(dǎo)說可能需要做,就又做了一次。雖然不是什么很難的知識點,這里也就記錄一下自己做的過程吧。

  • info.plist 文件編輯

  • 更換 Icon

  • 靜默切換

info.plist 文件

為了動態(tài)更換 icon,我們需要先配置一下我們項目的 info.plist 文件:

iOS中怎么實現(xiàn)動態(tài)更換Icon

1、加入 Icon files(iOS5),其中會默認(rèn)有兩個 item:

  • Newsstand Icon

  • Primary Icon

2、我們需要加入我們需要的鍵——CFBundleAlternateIcons,類型為 Dictionary。

3、下面再添加一些字典。這里字典的鍵是你希望更換 Icon 的名稱,在下方的 CFBundleIconFiles 數(shù)組中,寫入需要更換的 Icon 的名稱。

Primary Icon: 可以設(shè)置 App 的主 Icon,一般都不理會。一般主 Icon 在 Assets.xcassets 中設(shè)置。

Newsstand Icon: 這個設(shè)置一般用于在 Newsstand 中顯示使用。我們也不需要理會。

這里我們就將 info.plist 編輯完成了,下面我們將對應(yīng)的圖片加入到項目中,這里的圖片需要直接加到項目中,不能放在 Assets.xcassets 中。

iOS中怎么實現(xiàn)動態(tài)更換Icon

更換 Icon

在 iOS 10.3,蘋果開放了這個 API,可以讓我們動態(tài)更換我們的 App Icon。

// If false, alternate icons are not supported for the current process.
@available(iOS 10.3, *)
open var supportsAlternateIcons: Bool { get }
  
// Pass `nil` to use the primary application icon. The completion handler will be invoked asynchronously on an arbitrary background queue; be sure to dispatch back to the main queue before doing any further UI work.
@available(iOS 10.3, *)
open func setAlternateIconName(_ alternateIconName: String?, completionHandler: ((Error?) -> Void)? = nil)
  
// If `nil`, the primary application icon is being used.
@available(iOS 10.3, *)
open var alternateIconName: String? { get }

切換到我們需要的 Icon

@IBAction func changeOneClick(_ sender: Any) {
  if UIApplication.shared.supportsAlternateIcons {
    UIApplication.shared.setAlternateIconName("lambot") { (error) in
      if error != nil {
        print("更換icon錯誤")
      }
    }
  }
}

這里的 iconName 直接傳入項目中的 icon 名稱。這里需要注意的是,項目中的名字、info.plist 中存入的名稱以及這里傳入的名稱需要一致。

重置為原始的 Icon

@IBAction func resetClick(_ sender: Any) {
  if UIApplication.shared.supportsAlternateIcons {
    UIApplication.shared.setAlternateIconName(nil) { (error) in
      if error != nil {
        print("更換icon錯誤")
      }
    }
  }
}

如果需要恢復(fù)為原始的 icon,只需要在傳入 iconName 的地方傳入 nil 即可。

iOS中怎么實現(xiàn)動態(tài)更換Icon

現(xiàn)在,已經(jīng)完成了切換 Icon 的功能了。但是每次切換時,都會有一個彈框,下面我們就想辦法去掉這個彈框。

靜默切換

我們可以利用 Runtime 的方法來替換掉彈出提示框的方法。

以前 Method Swizzling 的時候需要在 load 或者 initialize 方法,但是在 Swift 中不能使用了。那就只能自己定義一個了。

extension UIViewController {
  public class func initializeMethod() {
    if self != UIViewController.self {
      return
    }
		// Method Swizzling
    DispatchQueue.once(token: "ChangeIcon") {
      let orignal = class_getInstanceMethod(self, #selector(UIViewController.present(_:animated:completion:)))
      let swizzling = class_getInstanceMethod(self, #selector(UIViewController.jt_present(_:animated:completion:)))

      if let old = orignal, let new = swizzling {
        method_exchangeImplementations(old, new)
      }
    }
  }

  @objc private func jt_present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
    // 在這里判斷是否是更換icon時的彈出框
    if viewControllerToPresent is UIAlertController {

      let alertTitle = (viewControllerToPresent as! UIAlertController).title
      let alertMessage = (viewControllerToPresent as! UIAlertController).message

      // 更換icon時的彈出框,這兩個string都為nil。
      if alertTitle == nil && alertMessage == nil {
        return
      }
    }
		
		// 因為方法已經(jīng)交換,這個地方的調(diào)用就相當(dāng)于調(diào)用原先系統(tǒng)的 present
    self.jt_present(viewControllerToPresent, animated: flag, completion: completion)
  }
}

定義完 UIViewController 的擴(kuò)展方法后,記得在 AppDelegate 中調(diào)用一下。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

  UIViewController.initializeMethod()

  return true
}

因為,Swift 中 GCD 之前的 once 函數(shù)沒有了,這里自己簡單定義了一個。

extension DispatchQueue {
  private static var _onceTracker = [String]()
  public class func once(token: String, block: () -> ()) {
    objc_sync_enter(self)
    defer {
      objc_sync_exit(self)
    }
    if _onceTracker.contains(token) {
      return
    }
    _onceTracker.append(token)
    block()
  }
}

defer block 里的代碼會在函數(shù) return 之前執(zhí)行,無論函數(shù)是從哪個分支 return 的,還是有 throw,還是自然而然走到最后一行。

看完上述內(nèi)容,你們對iOS中怎么實現(xiàn)動態(tài)更換Icon有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI