How can you detect if it is the first time a user opens an app [duplicate]

落爺英雄遲暮 提交于 2019-12-02 23:45:39

问题


Is it possible to detect if it is the first time a user opens an iOS application, using Objective-C?

I would like to show a welcome message when the user opens up the app for the first time, but not show it to them after that.

I'm looking for something like:

BOOL firstTime = [AppDelegate isFirstTimeOpeningApplication]

回答1:


Use NSUserDefaults

//In applicationDidFinishLaunching:withOptions:
BOOL hasLaunchedBefore = [[NSUserDefaults standardUserDefaults] boolForKey:@"AppHasLaunchedBefore"];
if(!hasLaunchedBefore) {
    //First launch
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"AppHasLaunchedBefore"];
}
else {
    //Not first launch
}

Hope that helps!




回答2:


Look for some value in your app's preferences, or the existence of some file. If you don't find it, your app is running for the first time, so write the expected value to the preferences or create the file so that the next time your app runs you'll know that it's not the first time.

If you store the time and date of the first run instead of just a flag, you can determine how long it's been since the app was used. You might want your app to act like it's the first run if the user hasn't used your app in a very long time.

Note that this technique only works if the user hasn't deleted your app. When an app is deleted, all its data is removed. If you want to know if your app has ever run on that device before, even if it was deleted afterward, you'll need to record information to identify the device elsewhere, such as on your own server.




回答3:


You can save a "I've run before" flag in NSUserDefaults.




回答4:


- (void)applicationDidFinishLaunching:(UIApplication *)application {
 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    NSString *firsttime = [defaults stringForKey:@"firsttime"];
    if (firsttime == nil) {

        TheOtherViewController *Other = [[TheOtherViewController alloc] initWithNibName:nil bundle:nil];
        Other.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [window addSubview: Other.view];        

        [defaults setObject:@"lasttime" forKey:@"firsttime"];
    }   else {  [window addSubview:viewController.view];
        [window makeKeyAndVisible];
    }
}



回答5:


Use NSUserDefaults to store a flag which will indicate if the user has launched the app before or not. If the value of flag .. lets say "IsFirstTimeLaunched" is false then it means the user has not launched the app before.



来源:https://stackoverflow.com/questions/18744386/how-can-you-detect-if-it-is-the-first-time-a-user-opens-an-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!