Wednesday, February 17, 2016

Objective C checking application first launch

We need to check this app first launch, If our app has something like of the below mentioned features.

1. Our app has to display the tour or guide screens like demo of our application on the first launch.

2. Our app gives some free points/stuff for the first launch.

etc.......

We can check whether application is launched for the first time or not in a device using the below methods. First in place, I want to convey like I am considering the user deleting the application and installing app again also as a first launch for these below methods.

#1 Using NSUserDefaults:


We use NSUserDefaults for temporary storage. We can check first launch like shown below using NSUserDefaults.



- (BOOL)isFirstLaunch {
    
    BOOL firstLaunch = YES;

    id val  = [NSUserDefaults standardUserDefaults] objectForKey:@"first_launch"];

    if(val != nil){
          firstLaunch = NO;
    }

    else{
         [NSUserDefaults standardUserDefaults] setObject:@"launched" 
                                                                                  forKey:@"first_launch"];
    }

    return firstLaunch;

}



If the app is a fresh launch, obviously the object in the 'first_launch' key will be nil. NSUserDefaults exist as long as the app is there in the device though we kill the running application in the device. After checking that condition, If it's a first launch, set some object in that key. So that next time it won't be nil when we check that condition again.


#2 Using NSFileManager by creating a temporary folder:


We can create a dummy folder after the first launch and use that folder exist condition to check the first launch.



- (BOOL)isFirstLaunch {
    
    BOOL firstLaunch = NO;

    NSString *folderName = @"first_launch";

    BOOL isDir;

    firstLaunch = [[NSFileManager defaultManagerfileExistsAtPath:[[AppData  
                 sharedAppDatagetDocumentsPathForFolder:folderName] isDirectory:&isDir];

    return !firstLaunch;


}


However, above two methods can only check a fresh install of a application. Means, they consider even deleting and installing the application again also as a first launch. What If we need to check the very first launch, The first time user installed the application in the device.

Here are the different methods to check the very first launch in the device.

#3 Using KeyChain:


The values we set in the device keychain won't get erased until the user reset their device. Though we delete the application from device, the values set in keychain won't get erased. So we can use this keychain to check the very first launch of our app by setting some value in the keychain from our app.

#4 Storing first launch in the server:


We can also store device unique id in the database through web services and check this value whenever user launches the app. As we are storing the device unique id, How many times the user downloads and installs the app, We can check that very first launch condition.



Hope this post is useful. Feel free to comment in case of any queries.

No comments:

Post a Comment