影片直播app原始碼,Swift動態修改Icon,消除系統彈窗

zhibo系統開發發表於2023-03-16

影片直播app原始碼,Swift動態修改Icon,消除系統彈窗

實現


1 匯入待替換的新圖片,放到專案工程新資料夾中;

2 配置 Info.plist 檔案:

<key>CFBundleIcons</key>
<dict>
    <key>CFBundleAlternateIcons</key>
    <dict>
        <key>icon1</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string>icon1</string>
            </array>
            <key>UIPrerenderedIcon</key>
            <false/>
        </dict>
        <key>icon2</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string>icon2</string>
            </array>
            <key>UIPrerenderedIcon</key>
            <false/>
        </dict>
    </dict>
    <key>CFBundlePrimaryIcon</key>
    <dict>
        <key>CFBundleIconFiles</key>
    <array>
        <string>AppIcon60x60</string>
    </array>
    </dict>
</dict>


3 透過程式碼替換

 

       if #available(iOS 10.3, *) {
            if UIApplication.shared.supportsAlternateIcons {
                print("you can change this app's icon")
            }else {
                print("you cannot change this app's icon")
                return
            }
            
            if let name = UIApplication.shared.alternateIconName {
                // CHANGE TO PRIMARY ICON  恢復預設 icon
                UIApplication.shared.setAlternateIconName(nil) { (err:Error?) in
                    print("set icon error:\(String(describing: err))")
                }
                print("the alternate icon's name is \(name)")
            }else {
                // CHANGE TO ALTERNATE ICON 指定icon圖示
                UIApplication.shared.setAlternateIconName("icon1") { (err:Error?) in
                    print("set icon error:\(String(describing: err))")
                }
            }
        }


4 去掉更換圖示時的彈框

更換圖示時會出現系統彈框,可以使用Runtime來隱藏彈框,這樣方便在節日時候程式自動 無感 更改APP 的icon

具體程式碼如下:

extension UIViewController {
  //透過執行時替換系統的present方法
  public class func nkReplaceSystemPresent(){
    let systemSelector = #selector(UIViewController.present(_:animated:completion:))
    let nkSelector = #selector(UIViewController.newPesent(_:animated:completion:))
    let systemMethod = class_getInstanceMethod(self, systemSelector)
    let nkNewMethod = class_getInstanceMethod(self, nkSelector)
      method_exchangeImplementations(systemMethod!, nkNewMethod!)
  }
  @objc public func newPesent(_ vcToPresent:UIViewController, animated flag:Bool, completion: (() ->Void)? = nil) {
    if vcToPresent.isKind(of:UIAlertController.self) {
      let alertController = vcToPresent as? UIAlertController
      if alertController?.title==nil && alertController?.message==nil {
        return
      }
    }
    self.newPesent(vcToPresent, animated: flag)
  }
}


然後在滿足需求的控制器中呼叫即可。

方法如下:

    override func viewDidLoad() {
        super.viewDidLoad()
        DH_MyViewController.nkReplaceSystemPresent()
    }


 以上就是 影片直播app原始碼,Swift動態修改Icon,消除系統彈窗,更多內容歡迎關注之後的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2940045/,如需轉載,請註明出處,否則將追究法律責任。

相關文章