今天就跟大家聊聊有關(guān)iOS中怎么實(shí)現(xiàn)動(dòng)態(tài)更換Icon,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
成都創(chuàng)新互聯(lián)公司主要業(yè)務(wù)有網(wǎng)站營銷策劃、成都做網(wǎng)站、網(wǎng)站制作、微信公眾號(hào)開發(fā)、小程序定制開發(fā)、成都h5網(wǎng)站建設(shè)、程序開發(fā)等業(yè)務(wù)。一次合作終身朋友,是我們奉行的宗旨;我們不僅僅把客戶當(dāng)客戶,還把客戶視為我們的合作伙伴,在開展業(yè)務(wù)的過程中,公司還積累了豐富的行業(yè)經(jīng)驗(yàn)、營銷型網(wǎng)站建設(shè)資源和合作伙伴關(guān)系資源,并逐漸建立起規(guī)范的客戶服務(wù)和保障體系。
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 編輯完成了,下面我們將對應(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ù)沒有了,這里自己簡單定義了一個(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,還是自然而然走到最后一行。
看完上述內(nèi)容,你們對iOS中怎么實(shí)現(xiàn)動(dòng)態(tài)更換Icon有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
網(wǎng)頁標(biāo)題:iOS中怎么實(shí)現(xiàn)動(dòng)態(tài)更換Icon
網(wǎng)站路徑:http://muchs.cn/article18/jpggdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、關(guān)鍵詞優(yōu)化、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航、搜索引擎優(yōu)化、域名注冊
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)