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 running, but I wanted to know if there is a better way to do this.
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- how do you prevent page scroll in textarea on mobi
- Custom UITableview cell accessibility not working
相关文章
- Could I create “Call” button in HTML 5 IPhone appl
- Unable to process app at this time due to a genera
- How do you detect key up / key down events from a
- “Storyboard.storyboard” could not be opened
- Open iOS 11 Files app via URL Scheme or some other
- Can keyboard of type UIKeyboardTypeNamePhonePad be
- Can not export audiofiles via “open in:” from Voic
- XCode 4.5 giving me “SenTestingKit/SenTestKit.h” f
Swift:
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.
I like to use NSUserDefaults to store an indication of the the first run.
You can then test for it later...
Ok what confuses the hell out of me about User Defaults.
WHERE are they stored?
This is the common use case: Checking for the existence of a value e.g firstRun. The first time it will NOT EXIST so usually followed by setting the value. 2nd Run - on next loop it does exist and other use case/else stmt is triggered
---- .h
------ .m
//NOTE IMPORTANT IF YOURE ROOTVIEWCONTROLLER checks appDelegate.firstRun then make sure you do the check above BEFORE setting self.window.rootViewController here
---- USING THE FLAG
The examples above confused me a bit as they show how to check for it the first time but then mention how to 'check for it later' in the same comment. The problem is when we find it doesnt exist we immediately create it and synchronize. So checking for it late actually mean when you RESTART THE APP not in same run as first run.
In your app delegate register a default value:
Then where you want to check it:
You can implement it with the static method below. I think it's better since you can call this method as many times as you like, unlike the other solutions. enjoy: (Keep in mind that it's not thread-safe)
You can use a custom category method isFirstLaunch with UIViewController+FirstLaunch.
And when you need to use it in controller