Best way to check if an iPhone app is running for the first time

后端 未结 7 1198
萌比男神i
萌比男神i 2020-11-28 22:19

I want to check if my iPhone app is running for the first time. I can create a file in the documents folder and check that file to see if this is the first time the app is r

7条回答
  •  抹茶落季
    2020-11-28 22:39

    Swift:

    var isFirstLaunch: Bool {
        get {
            if (NSUserDefaults.standardUserDefaults().objectForKey("firstLaunchDate") == nil) {
                NSUserDefaults.standardUserDefaults().setObject(NSDate(), forKey: "firstLaunchDate")
                NSUserDefaults.standardUserDefaults().synchronize()
                return true
            }
            return false
        }
    }
    

    Another tip:

    When using NSUserDefaults, these settings will be wiped if the app is ever deleted. If for some reason you require these settings to still hang around, you can store them in the Keychain.

提交回复
热议问题