iOS動(dòng)態(tài)更換Icon的全過程記錄
iOS 動(dòng)態(tài)更換Icon
動(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 文件:
1、加入 Icon files(iOS5),其中會(huì)默認(rèn)有兩個(gè) item:
- Newsstand Icon
- Primary Icon
2、我們需要加入我們需要的鍵——CFBundleAlternateIcons,類型為 Dictionary。
3、下面再添加一些字典。這里字典的鍵是你希望更換 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 }
切換到我們需要的 Icon
@IBAction func changeOneClick(_ sender: Any) { if UIApplication.shared.supportsAlternateIcons { UIApplication.shared.setAlternateIconName("lambot") { (error) in if error != nil { print("更換icon錯(cuò)誤") } } } }
這里的 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ù)為原始的 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) } }
定義完 UIViewController 的擴(kuò)展方法后,記得在 AppDelegate 中調(diào)用一下。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { UIViewController.initializeMethod() return true }
因?yàn)?,Swift 中 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() } }
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)。
好了,以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)我們的支持。
參考鏈接:
Information Property List Key Reference
欄 目:IOS
下一篇:iOS調(diào)試Block引用對(duì)象無法被釋放的小技巧分享
本文標(biāo)題:iOS動(dòng)態(tài)更換Icon的全過程記錄
本文地址:http://mengdiqiu.com.cn/a1/IOS/11887.html
您可能感興趣的文章
- 01-11iOS常用算法之兩個(gè)有序數(shù)組合并(要求時(shí)間復(fù)雜度為0(n))
- 01-11iOS 彈幕功能的實(shí)現(xiàn)思路圖解
- 01-11iOS調(diào)試Block引用對(duì)象無法被釋放的小技巧分享
- 01-11iOS實(shí)現(xiàn)文本分頁的方法示例
- 01-11iOS常見宏理解及使用方法
- 01-11iOs遷至WKWebView跨過的一些坑
- 01-11iOS模擬中獎(jiǎng)名單循環(huán)滾動(dòng)效果
- 01-11Python一鍵查找iOS項(xiàng)目中未使用的圖片、音頻、視頻資源
- 01-11iOS中如何獲取某個(gè)視圖的截圖詳析
- 01-11iOS13適配的實(shí)現(xiàn)方法


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-11UILabel顯示定時(shí)器文本跳動(dòng)問題的解決
- 01-11iOS常用算法之兩個(gè)有序數(shù)組合并(要
- 01-11iOS 彈幕功能的實(shí)現(xiàn)思路圖解
- 01-11詳解MacOs免密登錄CentOs操作步驟
- 01-11iOS動(dòng)態(tài)更換Icon的全過程記錄
- 01-11iOS調(diào)試Block引用對(duì)象無法被釋放的小技
- 01-11iOS常見宏理解及使用方法
- 01-11iOS實(shí)現(xiàn)文本分頁的方法示例
- 01-11iOs遷至WKWebView跨過的一些坑
- 01-11iOS模擬中獎(jiǎng)名單循環(huán)滾動(dòng)效果
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子