简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM