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

后端 未结 7 1219
萌比男神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条回答
  •  旧时难觅i
    2020-11-28 22:45

    You can use a custom category method isFirstLaunch with UIViewController+FirstLaunch.

    - (BOOL)isFirstLaunch
    {
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"kFirstLaunch"]) {
            return YES;
        }
        else {
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"kFirstLaunch"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            return NO;
        }
    }
    

    And when you need to use it in controller

    BOOL launched = [self isFirstLaunch];
    
    if (launched) {
    //if launched
    }
    else {
    //if not launched
    }
    

提交回复
热议问题