动态切换 App 的 icon 这个需求,在上一家公司作一款定制 App 时遇到过一次,此次领导说可能须要作,就又作了一次。虽然不是什么很难的知识点,这里也就记录一下本身作的过程吧。html
为了动态更换 icon,咱们须要先配置一下咱们项目的 info.plist 文件:数组
Primary Icon: 能够设置 App 的主 Icon,通常都不理会。通常主 Icon 在 Assets.xcassets 中设置。bash
Newsstand Icon: 这个设置通常用于在 Newsstand 中显示使用。咱们也不须要理会。app
这里咱们就将 info.plist 编辑完成了,下面咱们将对应的图片加入到项目中,这里的图片须要直接加到项目中,不能放在 Assets.xcassets 中。async
在 iOS 10.3,苹果开放了这个 API,可让咱们动态更换咱们的 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 }
复制代码
@IBAction func changeOneClick(_ sender: Any) {
if UIApplication.shared.supportsAlternateIcons {
UIApplication.shared.setAlternateIconName("lambot") { (error) in
if error != nil {
print("更换icon错误")
}
}
}
}
复制代码
这里的 iconName 直接传入项目中的 icon 名称。这里须要注意的是,项目中的名字、info.plist 中存入的名称以及这里传入的名称须要一致。学习
@IBAction func resetClick(_ sender: Any) {
if UIApplication.shared.supportsAlternateIcons {
UIApplication.shared.setAlternateIconName(nil) { (error) in
if error != nil {
print("更换icon错误")
}
}
}
}
复制代码
若是须要恢复为原始的 icon,只须要在传入 iconName 的地方传入 nil 便可。ui
如今,已经完成了切换 Icon 的功能了。可是每次切换时,都会有一个弹框,下面咱们就想办法去掉这个弹框。spa
咱们能够利用 Runtime 的方法来替换掉弹出提示框的方法。3d
之前 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
}
}
// 由于方法已经交换,这个地方的调用就至关于调用原先系统的 present
self.jt_present(viewControllerToPresent, animated: flag, completion: completion)
}
}
复制代码
定义完 UIViewController 的扩展方法后,记得在 AppDelegate 中调用一下。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIViewController.initializeMethod()
return true
}
复制代码
由于,Swift 中 GCD 以前的 once 函数没有了,这里本身简单定义了一个。
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 的时候,就不会出现弹出框了。
简单的知识点,时间长了不用也有可能忘记。但愿本身能坚持学习,坚持记录,不断成长。
参考连接: