gpt4 book ai didi

ios - 识别用户是否是第一次升级或安装新版本的应用程序

转载 作者:行者123 更新时间:2023-11-29 10:43:11 25 4
gpt4 key购买 nike

我想知道我的用户是第一次下载我的应用还是升级旧版本。

我如何在应用程序启动时获取该信息?

最佳答案

选项 1。

将捆绑版本保存在某处并检查它是否不同于

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

在每次应用启动时。

选项 2。

在 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

关于ios - 识别用户是否是第一次升级或安装新版本的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23442641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com