The popular solution is to use [Nsuserdefaults standarduserdefaults a key in most places, if it doesn't exist, which means that this is the application startup, otherwise it's not the first time. Then it says set the key to the latter in the method: "Applicationwillterminate: (uiapplication*) Application". This solution is very good until we get to iOS 4.0. In iOS 4.0 + This approach is rarely known (only for memory issues) as described in Apple's uiapplicationdelegate documentation. ...
You can search for the key if it is not set to run First-time-code and set it, but in the rest of the app you will not know if it is the first run (unless you use a global variable)
Therefore, I recommend my solution: ...
In APPDELEGATE.M, locate the "Application:didfinishlaunchingwithoptions:" Method and add the following code:
[HTML]View Plaincopy < param name= "wmode" value= "Transparent" >
- if (![ [Nsuserdefaults Standarduserdefaults] boolforkey:@ "everlaunched"]) {
- [[Nsuserdefaults Standarduserdefaults] setbool:yes forkey:@ "everlaunched"];
- [[Nsuserdefaults Standarduserdefaults] setbool:yes forkey:@ "Firstlaunch"];
- }
- else{
- [[Nsuserdefaults Standarduserdefaults] setbool:no forkey:@ "Firstlaunch"];
- }
The total solution is 2 key: @ "everlaunched" to determine if the user has previously logged in,
@ "Firstlaunch" is used by developers to judge in other parts of the program.
At the first boot, key @ "everlaunched" is not assigned and is set to Yes. @ "Firstlaunch" is set to YES.
In other parts of the program, use the following code to determine:
[HTML]View Plaincopy
- if ([[[Nsuserdefaults Standarduserdefaults] boolforkey:@ "Firstlaunch"]) {
- Here to determine whether the first time
- Uialertview *Alert=[[uialertview alloc] initwithtitle:@ "First time"
- message:@ "Go to App"
- Delegate:self
- cancelbuttontitle:@ "I know."
- Otherbuttontitles:nil];
- [Alert show];
- [Alert release];
- }
The first piece of code running key @ "Firstlaunch" will be set to NO.