如何确定第一次安装或使用应用程序的日期?

fel*_*ace 17 iphone ios4

我打算开设一个应用程序商店,我想免费为现有用户提供一些项目.

我想发布一个更新,它将在第一次使用该应用程序时存储一些信息,然后发布"真实"更新,看看之前是否购买过该应用程序,但是,很可能不是每个人都会选择第一次更新.

那么,有没有办法找出用户何时首次安装(或使用)应用程序?

更新:

谢谢你的答案,但我应该减少它的含糊:

我正在寻找原生电话/任何类似的事情.由于应用程序已经在商店,我没有设置任何东西来存储第一个版本的数据,如果所有用户在第二个更新发布之前抓住它,更新将帮助我做我想要的:它是无法区分新用户与错过中间更新的现有用户,并且刚刚更新到最新用户.

Dre*_*w H 37

要获取安装日期,请检查Documents文件夹的创建日期.

NSURL* urlToDocumentsFolder = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
__autoreleasing NSError *error;
NSDate *installDate = [[[NSFileManager defaultManager] attributesOfItemAtPath:urlToDocumentsFolder.path error:&error] objectForKey:NSFileCreationDate];

NSLog(@"This app was installed by the user on %@", installDate);
Run Code Online (Sandbox Code Playgroud)

要获取上次应用更新的日期,请检查应用包本身的修改日期

NSString* pathToInfoPlist = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
NSString* pathToAppBundle = [pathToInfoPlist stringByDeletingLastPathComponent];
NSDate *updateDate  = [[[NSFileManager defaultManager] attributesOfItemAtPath:pathToAppBundle error:&error] objectForKey:NSFileModificationDate];

NSLog(@"This app was updated by the user on %@", updateDate);
Run Code Online (Sandbox Code Playgroud)

  • 为了社区的利益,我正在提出一个由[Boris](/sf/users/361760311/)发布的答案,该答案应该是一个评论.由于代表他们不能,但它是针对这个答案; _由于我还没有评论,因为我还没有足够的代表,所以Drew H的答案适用于ios版本10.2,它会中断 - 应用程序包的修改日期返回0(01/01/1970)_ (4认同)

Jus*_*tin 11

NSDate *installDate = [[NSUserDefaults standardUserDefaults]objectForKey:@"installDate"];

if (!installDate) {
    //no date is present
    //this app has not run before
    NSDate *todaysDate = [NSDate date];
    [[NSUserDefaults standardUserDefaults]setObject:todaysDate forKey:@"installDate"];
    [[NSUserDefaults standardUserDefaults]synchronize];

    //nothing more to do?
} else {
    //date is found
    NSDate *todaysDate = [NSDate date];
    //compare todaysDate with installDate
    //if enough time has passed, yada yada,
    //apply updates, etc.
}
Run Code Online (Sandbox Code Playgroud)


Bra*_*don 10

我的解决方案是检查应用程序包中其中一个文件的上次修改日期.

NSString *sourceFile = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Icon.png"];

NSDate *lastModif = [[[NSFileManager defaultManager] attributesOfItemAtPath:sourceFile error:&err] objectForKey:NSFileModificationDate];
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,如果您必须使用更新执行此操作,因为它将在更新后显示新日期,这不起作用:( (3认同)

sam*_*ize 8

使用文档目录的创建日期是迄今为止最好的解决方案。在 Swift 5.0 中重现它

var inferredDateInstalledOn: Date? {
    guard
        let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last,
        let attributes = try? FileManager.default.attributesOfItem(atPath: documentsURL.path)
    else { return nil }
    return attributes[.creationDate] as? Date
}
Run Code Online (Sandbox Code Playgroud)