我打算开设一个应用商店,我想免费为现有用户提供一些商品。
我想发布一个更新,它会在第一次使用应用程序时存储一些信息,然后发布“真实”更新,看看之前是否购买了应用程序,但是,很可能不是每个人都会选择第一次更新。
那么,有没有办法找出用户何时首次安装(或已使用)应用程序?
更新:
感谢您的回答,但我应该不那么模糊:
我正在寻找原生电话/类似的事情。由于该应用已经在商店,我还没有设置任何内容来存储第一个版本的数据,如果所有用户在第二次更新之前抓取它,更新将帮助我做我想要的发布:无法区分新用户和错过中间更新的新用户,并且刚刚更新到最新用户。
答案 0 :(得分:35)
要获取安装日期,请检查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);
要获取上次应用更新的日期,请检查应用包本身的修改日期
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);
答案 1 :(得分: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.
}
答案 2 :(得分:10)
我的解决方案是检查应用包中其中一个文件的上次修改日期。
NSString *sourceFile = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Icon.png"];
NSDate *lastModif = [[[NSFileManager defaultManager] attributesOfItemAtPath:sourceFile error:&err] objectForKey:NSFileModificationDate];
答案 3 :(得分:1)
我建议您在首次启动时设置日期并将其存储在NSUserDefaults中。在发布时查找日期,如果不存在,请设置它。如果是,请进行一些日期比较,并根据您的计划处理其余的日期。
答案 4 :(得分:1)
回答看看人们是否可以找到这个问题。它似乎比我在这里找到的其他人更长,但它确实告诉你应用程序是否自更新后第一次运行(为此你必须更改你的info.plist)......
// called during startup, compares the current version string with the one stored on standardUserDefaults
+ (BOOL)isFreshInstallOrUpdate
{
BOOL ret = YES;
NSString *cfBundleVersionStored = [[NSUserDefaults standardUserDefaults] objectForKey:@"CFBundleVersion"];
NSString *cfBundleShortVersionStringStored = [[NSUserDefaults standardUserDefaults] objectForKey:@"CFBundleShortVersionString"];
NSString *cfBundleVersionPlist = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];;
NSString *cfBundleShortVersionStringPlist = [[NSBundle mainBundle] objectForInfoDictionaryKey:
@"CFBundleShortVersionString"];
ret = [cfBundleVersionPlist compare:cfBundleVersionStored] != NSOrderedSame ||
[cfBundleShortVersionStringPlist compare:cfBundleShortVersionStringStored] != NSOrderedSame;
return ret;
}
// calling this once will necessarily cause isFreshInstall to return false
+ (void)setStoredVersionNumber
{
NSString *cfBundleVersionPlist = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
NSString *cfBundleShortVersionStringPlist = [[NSBundle mainBundle] objectForInfoDictionaryKey:
@"CFBundleShortVersionString"];
[[NSUserDefaults standardUserDefaults] setObject:cfBundleVersionPlist forKey:@"CFBundleVersion"];
[[NSUserDefaults standardUserDefaults] setObject:cfBundleShortVersionStringPlist forKey:@"CFBundleShortVersionString"];
// setting now as unfresh!
[[NSUserDefaults standardUserDefaults] synchronize];
}
答案 5 :(得分:1)
应用构建日期:
public var appBuildDate: Date {
if let path = Bundle.main.path(forResource: "Info", ofType: "plist") {
if let createdDate = try! FileManager.default.attributesOfItem(atPath: path)[.creationDate] as? Date {
return createdDate
}
}
return Date() // Should never execute
}
应用安装日期:
public var appInstallDate: Date {
if let documentsFolder = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last {
if let installDate = try! FileManager.default.attributesOfItem(atPath: documentsFolder.path)[.creationDate] as? Date {
return installDate
}
}
return Date() // Should never execute
}
答案 6 :(得分:0)
您可以随时检查服务器。以前使用UDID很简单,但由于这些已被弃用,您已经创建了一个UUID(CFUUIDCreate()或类似的东西:))并将其存储在钥匙串中,即使用户将删除密钥链,存储在钥匙串中的每个信息都将保持不变。应用程序并在一年左右后再次安装...我发现这个articke在第一次处理钥匙串值时非常有用: http://useyourloaf.com/blog/2010/3/29/simple-iphone-keychain-access.html
答案 7 :(得分:0)
Swift solution:
let urlToDocumentsFolder = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).last!
//installDate is NSDate of install
let installDate = (try! NSFileManager.defaultManager().attributesOfItemAtPath(urlToDocumentsFolder.path!)[NSFileCreationDate])
print("This app was installed by the user on \(installDate)")
答案 8 :(得分:0)
使用Documents目录的创建日期是迄今为止最好的解决方案。在Swift 4.2中复制它
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
}
答案 9 :(得分:0)
这是一种更安全的方法,无需进行强制尝试和强制解包,并进行更新以适应 SWIFT 5 :
if
let urlToDocumentsFolder = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last,
let installDateAny = (try? FileManager.default.attributesOfItem(atPath: urlToDocumentsFolder.path)[.creationDate]),
let installDate = installDateAny as? Date
{
print("This app was installed by the user on \(installDate)")
}