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

后端 未结 7 1203
萌比男神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:40

    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)

    + (BOOL)isFirstTime{
        static BOOL flag=NO;
        static BOOL result;
        if(!flag){
            if ([[NSUserDefaults standardUserDefaults] boolForKey:@"hasLaunchedOnce"])
            {
                result=NO;
            } else
            {
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasLaunchedOnce"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                result=YES;
            }
    
        flag=YES;
        }
        return result;
    }
    

提交回复
热议问题