動(dòng)態(tài)切換 App 的 icon 這個(gè)需求莫换,在上一家公司做一款定制 App 時(shí)遇到過一次,這次領(lǐng)導(dǎo)說可能需要做,就又做了一次胚委。雖然不是什么很難的知識(shí)點(diǎn)竹椒,這里也就記錄一下自己做的過程吧。
- info.plist 文件編輯
- 更換 Icon
- 靜默切換
info.plist 文件
為了動(dòng)態(tài)更換 icon膝晾,我們需要先配置一下我們項(xiàng)目的 info.plist 文件:
- 加入 Icon files(iOS5),其中會(huì)默認(rèn)有兩個(gè) item:
- Newsstand Icon
- Primary Icon
- 我們需要加入我們需要的鍵——CFBundleAlternateIcons务冕,類型為 Dictionary血当。
- 下面再添加一些字典。這里字典的鍵是你希望更換 Icon 的名稱禀忆,在下方的 CFBundleIconFiles 數(shù)組中臊旭,寫入需要更換的 Icon 的名稱。
Primary Icon: 可以設(shè)置 App 的主 Icon箩退,一般都不理會(huì)离熏。一般主 Icon 在 Assets.xcassets 中設(shè)置。
Newsstand Icon: 這個(gè)設(shè)置一般用于在 Newsstand 中顯示使用戴涝。我們也不需要理會(huì)滋戳。
這里我們就將 info.plist 編輯完成了,下面我們將對(duì)應(yīng)的圖片加入到項(xiàng)目中啥刻,這里的圖片需要直接加到項(xiàng)目中奸鸯,不能放在 Assets.xcassets 中。
更換 Icon
在 iOS 10.3可帽,蘋果開放了這個(gè) API娄涩,可以讓我們動(dòng)態(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 }
復(fù)制代碼
切換到我們需要的 Icon
@IBAction func changeOneClick(_ sender: Any) {
if UIApplication.shared.supportsAlternateIcons {
UIApplication.shared.setAlternateIconName("lambot") { (error) in
if error != nil {
print("更換icon錯(cuò)誤")
}
}
}
}
復(fù)制代碼
這里的 iconName 直接傳入項(xiàng)目中的 icon 名稱映跟。這里需要注意的是蓄拣,項(xiàng)目中的名字、info.plist 中存入的名稱以及這里傳入的名稱需要一致努隙。
重置為原始的 Icon
@IBAction func resetClick(_ sender: Any) {
if UIApplication.shared.supportsAlternateIcons {
UIApplication.shared.setAlternateIconName(nil) { (error) in
if error != nil {
print("更換icon錯(cuò)誤")
}
}
}
}
復(fù)制代碼
如果需要恢復(fù)為原始的 icon球恤,只需要在傳入 iconName 的地方傳入 nil 即可。
現(xiàn)在剃法,已經(jīng)完成了切換 Icon 的功能了碎捺。但是每次切換時(shí),都會(huì)有一個(gè)彈框贷洲,下面我們就想辦法去掉這個(gè)彈框收厨。
靜默切換
我們可以利用 Runtime 的方法來替換掉彈出提示框的方法。
以前 Method Swizzling 的時(shí)候需要在 load 或者 initialize 方法优构,但是在 Swift 中不能使用了诵叁。那就只能自己定義一個(gè)了。
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時(shí)的彈出框
if viewControllerToPresent is UIAlertController {
let alertTitle = (viewControllerToPresent as! UIAlertController).title
let alertMessage = (viewControllerToPresent as! UIAlertController).message
// 更換icon時(shí)的彈出框钦椭,這兩個(gè)string都為nil拧额。
if alertTitle == nil && alertMessage == nil {
return
}
}
// 因?yàn)榉椒ㄒ呀?jīng)交換碑诉,這個(gè)地方的調(diào)用就相當(dāng)于調(diào)用原先系統(tǒng)的 present
self.jt_present(viewControllerToPresent, animated: flag, completion: completion)
}
}
復(fù)制代碼
定義完 UIViewController 的擴(kuò)展方法后,記得在 AppDelegate 中調(diào)用一下侥锦。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIViewController.initializeMethod()
return true
}
復(fù)制代碼
因?yàn)榻裕琒wift 中 GCD 之前的 once 函數(shù)沒有了,這里自己簡(jiǎn)單定義了一個(gè)恭垦。
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()
}
}
復(fù)制代碼
defer block 里的代碼會(huì)在函數(shù) return 之前執(zhí)行快毛,無論函數(shù)是從哪個(gè)分支 return 的,還是有 throw番挺,還是自然而然走到最后一行唠帝。
現(xiàn)在,我們?cè)俑鼡Q Icon 的時(shí)候玄柏,就不會(huì)出現(xiàn)彈出框了襟衰。
總結(jié)
簡(jiǎn)單的知識(shí)點(diǎn),時(shí)間長(zhǎng)了不用也有可能忘記粪摘。希望自己能堅(jiān)持學(xué)習(xí)瀑晒,堅(jiān)持記錄,不斷成長(zhǎng)赶熟。
參考鏈接:
Information Property List Key Reference
收錄:原文地址