欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

IOS

當(dāng)前位置:主頁 > 軟件編程 > IOS >

iOS動(dòng)態(tài)更換Icon的全過程記錄

來源:本站原創(chuàng)|時(shí)間:2020-01-11|欄目:IOS|點(diǎn)擊: 次

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實(shí)現(xiàn)文本分頁的方法示例

欄    目:IOS

下一篇:iOS調(diào)試Block引用對(duì)象無法被釋放的小技巧分享

本文標(biāo)題:iOS動(dòng)態(tài)更換Icon的全過程記錄

本文地址:http://mengdiqiu.com.cn/a1/IOS/11887.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有