如何确定iOS应用程序是新安装还是已更新?

8

我有一个应用程序已经上架到应用商店,我打算很快提交更新。

在此更新中,我想添加代码,以告诉应用程序在第一次运行时application:didFinishLaunchingWithOptions是:

  1. 从应用商店新安装的
  2. 从之前的版本进行了更新

目前应用程序没有处理这个问题的代码。
应用程序使用SQLite数据库,但由于某些原因我不想将其存在性作为解决此问题的方法。

另外一个问题是,是否有SDK可以查询应用程序何时安装到设备上,而无需手动存储数据?(最好兼容iOS 3.0)

我看过一个类似的问题,但没有一个答案适用于现有的应用商店代码。

5个回答

19
以下代码可以帮助回答您关于应用程序安装时间的副问题。我不确定应用程序捆绑创建日期是XCode构建日期还是下载日期,因为这在App Store中尚未经过测试。

以下代码可能有助于回答您的副问题,即关于应用程序安装时间的问题。我不确定应用程序包创建日期是XCode构建日期还是下载日期,因为这还未在应用商店中进行测试。

NSString *bundleRoot = [[NSBundle mainBundle] bundlePath]; // e.g. /var/mobile/Applications/<GUID>/<AppName>.app
NSFileManager *manager = [NSFileManager defaultManager];
NSDictionary* attrs = [manager attributesOfItemAtPath:bundleRoot error:nil];
NSLog(@"Build or download Date/Time of first  version to be installed: %@", [attrs fileCreationDate]);
NSLog(@"Date/Time of last install (unless bundle changed by code): %@", [attrs fileModificationDate]);
NSString *rootPath = [bundleRoot substringToIndex:[bundleRoot rangeOfString:@"/" options:NSBackwardsSearch].location]; // e.g /var/mobile/Applications/<GUID>
attrs = [manager attributesOfItemAtPath:rootPath error:nil];
NSLog(@"Date/Time first installed (or first reinstalled after deletion): %@", [attrs fileCreationDate]);

感谢这种新的方式,即使在之前的版本中没有NSUserDefaults设置,我们也可以找到是否有新的安装。 - Alex
2
我使用一个现有的应用商店应用进行测试,似乎第一条日志显示的是下载日期/时间而不是构建日期/时间。 - Alex
2
这是一个非常好的答案!应该被标记为正确答案。 - Vinod Vishwanath
2
为了社区的利益,我正在转移Boris发布的一个应该是评论的答案。由于他们的声望不够,所以他们不能发表评论。这个答案是针对以下回答的; _由于我还没有足够的声望,无法发表评论,Andy Bradbrook的答案在iOS 10.2之前都有效,但在此版本中会出现问题——应用程序包的修改日期返回0(1970年1月1日)_。 - Bugs

7
您可以将版本号保存到NSUserDefaults中,并相应地更新它。
如果这不起作用,您可能能够发布一个介绍版本控制方案的中间版本。
如果这不是一个选项,您可以检查您创建的文件或有条件或懒惰设置的首选项中以前运行的痕迹。

4
所以问题是什么?没有版本号 == 旧版本。 - Krizz
在某些情况下,您可以在中间版本中引入版本方案。您能想到任何在第一次启动时不存在的持久数据或首选项键吗? - justin

3

试试这段代码,虽然我回答晚了,但为了分享知识,我还是来了。

    -(void) checkIsAppUpdated
{
NSString *urlString = @"http://itunes.apple.com/lookup?id=995558215";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];

[request setHTTPMethod:@"GET"];

NSError *error = nil;
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

//NSString *stringReply = (NSString *)[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

if (error)
{
   // NSLog(@"Error: %@", stringReply);
    //error reviced

}
else
{
    //The response is in data
    //NSLog(@"Success: %@", stringReply);
    NSDictionary *dictResponse = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    float appStoreVersion=[[[[dictResponse objectForKey:@"results"] firstObject] objectForKey:@"version"] floatValue];

    NSLog(@"app stroe version=%f",appStoreVersion);

    NSString *strLocalVersion=[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    float localAppVersion=[strLocalVersion floatValue];
    if (localAppVersion!=appStoreVersion)
    {
        //open app store url
       // NSString *iTunesLink = @"itms://itunes.apple.com/us/app/apple-store/id375380948?mt=8";
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/app/washthenfold/id995558215?mt=8"]];
    }
}

 }

注意

id:将id替换为您自己的应用程序ID。您可以从iTunes Connect选择的应用程序 ->更多信息 ->元数据中获取应用程序ID。

由于模拟器没有应用商店应用程序,因此此代码在模拟器上不起作用。


0

这里有一个简单的代码,可以判断当前版本是否不同(该代码也适用于模拟器)。

-(BOOL) needsUpdate
{
    NSDictionary* infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString* appID = infoDictionary[@"CFBundleIdentifier"];
    NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?bundleId=%@", appID]];
    NSData* data = [NSData dataWithContentsOfURL:url];
    NSDictionary* lookup = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

    if ([lookup[@"resultCount"] integerValue] == 1)
    {
        NSString* appStoreVersion = lookup[@"results"][0][@"version"];
        NSString* currentVersion = infoDictionary[@"CFBundleShortVersionString"];
        if (![appStoreVersion isEqualToString:currentVersion])
        {
            NSLog(@"Need to update [%@ != %@]", appStoreVersion, currentVersion);
            return YES;
        }
    }
    return NO;
}

注意:确保在iTunes中输入新版本时与您发布的应用程序中的版本相匹配。如果不匹配,则上述代码将始终返回YES,无论用户是否更新。

0

GBVersionTracking 是一个很好的pod,可以跟踪所有版本历史记录。

[GBVersionTracking isFirstLaunchEver];  

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接