Determine if the app starts for the first time

Source: Internet
Author: User

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" >
    1. if (![ [Nsuserdefaults Standarduserdefaults] boolforkey:@ "everlaunched"]) {
    2. [[Nsuserdefaults Standarduserdefaults] setbool:yes forkey:@ "everlaunched"];
    3. [[Nsuserdefaults Standarduserdefaults] setbool:yes forkey:@ "Firstlaunch"];
    4. }
    5. else{
    6. [[Nsuserdefaults Standarduserdefaults] setbool:no forkey:@ "Firstlaunch"];
    7. }

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
  1. if ([[[Nsuserdefaults Standarduserdefaults] boolforkey:@ "Firstlaunch"]) {
  2. Here to determine whether the first time
  3. Uialertview *Alert=[[uialertview alloc] initwithtitle:@ "First time"
  4. message:@ "Go to App"
  5. Delegate:self
  6. cancelbuttontitle:@ "I know."
  7. Otherbuttontitles:nil];
  8. [Alert show];
  9. [Alert release];
  10. }

The first piece of code running key @ "Firstlaunch" will be set to NO.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.