Swift - 判斷應用是否是第一次啟動(或當前版本是否第一次啟動)


1 實現原理

(1)我們會發現許多 App 在一次啟動時會顯示一個新手引導頁(下次啟動就不會再顯示)
 
(2)其判斷原理就是在 AppDelegate 里的 didFinishLaunchingWithOptions 方法中檢查 UserDefaults 中是否存在特定的鍵值:
  • 不存在則說明是第一次運行,我們便把根視圖控制器改成引導頁,並保存這個特定的鍵值(Bool 類型即可)。
  • 已存在則說明之前已運行過該應用,那么就顯示默認視圖。
 
(3)有時我們還想在應用更新后,新版本第一次啟動時顯示個新功能說明頁,其原理同樣是判斷 UserDefaults 里的鍵值。只不過這次保存的是版本號,每次將之前保存的版本號與當前應用的版本號做比較:
  • 不同則說明新版本第一次啟動。
  • 相同則說明新版本之前已經啟動過。
 

2 樣例代碼

(1)為方便使用,這里對 UserDefaults 進行擴展,增加兩個判斷是否是第一次啟動的方法:
extension UserDefaults {
    //應用第一次啟動
    static func isFirstLaunch() -> Bool {
        let hasBeenLaunched = "hasBeenLaunched"
        let isFirstLaunch = !UserDefaults.standard.bool(forKey: hasBeenLaunched)
        if isFirstLaunch {
            UserDefaults.standard.set(true, forKey: hasBeenLaunched)
            UserDefaults.standard.synchronize()
        }
        return isFirstLaunch
    }
     
    //當前版本第一次啟動
    static func isFirstLaunchOfNewVersion() -> Bool {
        //主程序版本號
        let infoDictionary = Bundle.main.infoDictionary!
        let majorVersion = infoDictionary["CFBundleShortVersionString"] as! String
         
        //上次啟動的版本號
        let hasBeenLaunchedOfNewVersion = "hasBeenLaunchedOfNewVersion"
        let lastLaunchVersion = UserDefaults.standard.string(forKey:
            hasBeenLaunchedOfNewVersion)
         
        //版本號比較
        let isFirstLaunchOfNewVersion = majorVersion != lastLaunchVersion
        if isFirstLaunchOfNewVersion {
            UserDefaults.standard.set(majorVersion, forKey:
                hasBeenLaunchedOfNewVersion)
            UserDefaults.standard.synchronize()
        }
        return isFirstLaunchOfNewVersion
    }
}

 


(2)在 AppDelegate.swift 中調用上面的擴展方法進行判斷,並執行相應邏輯。

import UIKit
 
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
 
    var window: UIWindow?
 
    //程序啟動
    func application(_ application: UIApplication, didFinishLaunchingWithOptions
        launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
         
        //判斷當前版本是否第一次啟動
        if UserDefaults.isFirstLaunchOfNewVersion() {
            //顯示新功能介紹頁
            print("當前版本第一次啟動")
            let introductionViewController = IntroductionViewController()
            self.window!.rootViewController = introductionViewController
        }
         
        //判斷是否第一次啟動(兩個都是第一次則以這個為准)
        if UserDefaults.isFirstLaunch() {
            //顯示新手指導頁
            print("應用第一次啟動")
            let guideViewController = GuideViewController()
            self.window!.rootViewController = guideViewController
        }
 
        return true
    }
 
    func applicationWillResignActive(_ application: UIApplication) {
    }
 
    func applicationDidEnterBackground(_ application: UIApplication) {
    }
 
    func applicationWillEnterForeground(_ application: UIApplication) {
    }
 
    func applicationDidBecomeActive(_ application: UIApplication) {
    }
 
    func applicationWillTerminate(_ application: UIApplication) {
    }
}

 


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM