[英]Is their any way to identify user updating app and user installing app for the first time in iOS?
[英]Identify if users are upgrading or installing new version of app for the first time
我想知道我的用戶是第一次下載我的應用程序還是升級舊版本。
啟動應用程序后如何獲取該信息?
將捆綁軟件版本保存在某處,並檢查它是否與其他版本不同
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]]
在每次啟動應用時。
在UIApplication上使用類別,讓您查看應用程序是否已更新。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.