ios 動態更換icon
動態切換 app 的 icon 這個需求,在上一家公司做一款定制 app 時遇到過一次,這次領導說可能需要做,就又做了一次。雖然不是什么很難的知識點,這里也就記錄一下自己做的過程吧。
- info.plist 文件編輯
- 更換 icon
- 靜默切換
info.plist 文件
為了動態更換 icon,我們需要先配置一下我們項目的 info.plist 文件:
1、加入 icon files(ios5),其中會默認有兩個 item:
- newsstand icon
- primary icon
2、我們需要加入我們需要的鍵——cfbundlealternateicons,類型為 dictionary。
3、下面再添加一些字典。這里字典的鍵是你希望更換 icon 的名稱,在下方的 cfbundleiconfiles 數組中,寫入需要更換的 icon 的名稱。
primary icon: 可以設置 app 的主 icon,一般都不理會。一般主 icon 在 assets.xcassets 中設置。
newsstand icon: 這個設置一般用于在 newsstand 中顯示使用。我們也不需要理會。
這里我們就將 info.plist 編輯完成了,下面我們將對應的圖片加入到項目中,這里的圖片需要直接加到項目中,不能放在 assets.xcassets 中。
更換 icon
在 ios 10.3,蘋果開放了這個 api,可以讓我們動態更換我們的 app icon。
1
2
3
4
5
6
7
8
9
10
11
|
// 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
1
2
3
4
5
6
7
8
9
|
@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
1
2
3
4
5
6
7
8
9
|
@ibaction func resetclick(_ sender: any) { if uiapplication.shared.supportsalternateicons { uiapplication.shared.setalternateiconname(nil) { (error) in if error != nil { print( "更換icon錯誤" ) } } } } |
如果需要恢復為原始的 icon,只需要在傳入 iconname 的地方傳入 nil 即可。
現在,已經完成了切換 icon 的功能了。但是每次切換時,都會有一個彈框,下面我們就想辦法去掉這個彈框。
靜默切換
我們可以利用 runtime 的方法來替換掉彈出提示框的方法。
以前 method swizzling 的時候需要在 load 或者 initialize 方法,但是在 swift 中不能使用了。那就只能自己定義一個了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
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 } } // 因為方法已經交換,這個地方的調用就相當于調用原先系統的 present self.jt_present(viewcontrollertopresent, animated: flag, completion: completion) } } |
定義完 uiviewcontroller 的擴展方法后,記得在 appdelegate 中調用一下。
1
2
3
4
5
6
|
func application(_ application: uiapplication, didfinishlaunchingwithoptions launchoptions: [uiapplication.launchoptionskey: any]?) -> bool { uiviewcontroller.initializemethod() return true } |
因為,swift 中 gcd 之前的 once 函數沒有了,這里自己簡單定義了一個。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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 里的代碼會在函數 return 之前執行,無論函數是從哪個分支 return 的,還是有 throw,還是自然而然走到最后一行。
現在,我們再更換 icon 的時候,就不會出現彈出框了。
總結
簡單的知識點,時間長了不用也有可能忘記。希望自己能堅持學習,堅持記錄,不斷成長。
好了,以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。
參考鏈接:
information property list key reference
原文鏈接:https://juejin.im/post/5d3e4e936fb9a07ece681775