jigs
jigs

Reputation: 859

Identify if users are upgrading or installing new version of app for the first time

I want to know whether my users are downloading my application for the first time or upgrading the old version.

How can I get that information when application is launched?

Upvotes: 1

Views: 293

Answers (1)

Rafał Sroka
Rafał Sroka

Reputation: 40030

Option 1.

Save the bundle version somewhere and check if it differs from

[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]]

on each app startup.

Option 2.

Use a category on UIApplication that let's you see if the app was updated.

UIApplication+Versioning.h

@protocol UIApplicationDelegate<UIApplicationDelegate>
@optional
 
- (void)application:(UIApplication *)application 
willUpdateToVersion: (NSString*) newVersion 
        fromVersion: (NSString*) previousVersion;
         
- (void)application:(UIApplication *)application 
 didUpdateToVersion: (NSString*) newVersion 
        fromVersion: (NSString*) previousVersion;
 
@end
 
 
@interface UIApplication (Versioning)
 
@end

UIApplication+Versioning.m

#import "UIApplication+Versioning.h"
#import <objc/message.h>
#import <objc/runtime.h>
 
static NSString* UIApplicationVersionFileName = @"app.version";
 
@implementation UIApplication (Versioning)
 
+ (void)load 
{
    original = class_getInstanceMethod(self, @selector(setDelegate:));
    swizzled = class_getInstanceMethod(self, @selector(swizzled_setDelegate:));
 
    method_exchangeImplementations(original, swizzled);
}
 
 
- (void)swizzled_setDelegate:(id<UIApplicationDelegate>)delegate 
{
    IMP implementation = class_getMethodImplementation([self class], @selector(swizzled_application:didFinishLaunchingWithOptions:));
    class_addMethod([delegate class], @selector(swizzled_application:didFinishLaunchingWithOptions:), implementation, "B@:@@");
 
    original = class_getInstanceMethod([delegate class], @selector(application:didFinishLaunchingWithOptions:));
    swizzled = class_getInstanceMethod([delegate class], @selector(swizzled_application:didFinishLaunchingWithOptions:));
 
    method_exchangeImplementations(original, swizzled);
 
    [self swizzled_setDelegate: delegate];
}
 
 
- (BOOL)swizzled_application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
    // Check for a version change
    NSError* error;
    NSArray* directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* versionFilePath = [[directories objectAtIndex: 0] stringByAppendingPathComponent:UIApplicationVersionFileName];
    NSString* oldVersion = [NSString stringWithContentsOfFile:versionFilePath
                                                     encoding:NSUTF8StringEncoding
                                                        error:&error];
    NSString* currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"CFBundleVersion"];
 
    switch (error.code) 
    {
        case NSFileReadNoSuchFileError:
        {
            // Delegate methods will not be called first time
            oldVersion = [currentVersion copy];
            [currentVersion writeToFile: versionFilePath
                             atomically: YES
                               encoding: NSUTF8StringEncoding
                                  error: &error];
            break;
        }
        default:
        {
            NSLog(@"Warning: An error occured will loading the application version file -> Recreating file");
            [[NSFileManager defaultManager] removeItemAtPath: versionFilePath
                                                       error: nil];
            oldVersion = [currentVersion copy];
            [currentVersion writeToFile: versionFilePath
                             atomically: YES
                               encoding: NSUTF8StringEncoding
                                  error: &error];
            break;
        }
    }
 
    if( ![oldVersion isEqualToString: currentVersion] ) 
    {
        if ([[application delegate] respondsToSelector: @selector(application:willUpdateToVersion:fromVersion:)]) 
        {
            objc_msgSend([application delegate], @selector(application:willUpdateToVersion:fromVersion:), currentVersion, oldVersion);
        }
 
        [currentVersion writeToFile:versionFilePath
                         atomically:YES
                           encoding:NSUTF8StringEncoding
                              error:&error];
 
        if ([[application delegate] respondsToSelector: @selector(application:didUpdateToVersion:fromVersion:)]) 
        {
            objc_msgSend([application delegate], @selector(application:willUpdateToVersion:fromVersion:), currentVersion, oldVersion);
        }
    }
 
    SEL realSelector =  @selector(swizzled_application:didFinishLaunchingWithOptions:);
    return (BOOL) objc_msgSend([application delegate], realSelector, application, launchOptions);
}
 
 
@end

Upvotes: 2

Related Questions