Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何检查应用程序是否首次启动?_Objective C_Cocoa Touch - Fatal编程技术网

Objective c 如何检查应用程序是否首次启动?

Objective c 如何检查应用程序是否首次启动?,objective-c,cocoa-touch,Objective C,Cocoa Touch,我正在开发一个新版本的应用程序。在旧版本的应用程序中,一些内容已从web下载到缓存文件夹中。此内容在新版本中不再有效,因此我希望在新版本的应用程序首次启动时删除其中一些文件 如何检查应用程序是否首次启动?我可以使用NSUserDefaults来存储指示,但有更好的方法吗?NSUserDefaults方法没有错 if(the flag has not been set somewhere to say we've already done this dance){ do the stuff we

我正在开发一个新版本的应用程序。在旧版本的应用程序中,一些内容已从web下载到缓存文件夹中。此内容在新版本中不再有效,因此我希望在新版本的应用程序首次启动时删除其中一些文件


如何检查应用程序是否首次启动?我可以使用NSUserDefaults来存储指示,但有更好的方法吗?

NSUserDefaults方法没有错

if(the flag has not been set somewhere to say we've already done this dance){
 do the stuff we need to do 
 set the flag that we'll check again next time and skip this code next time
}

NSUserDefaults方法没有错

if(the flag has not been set somewhere to say we've already done this dance){
 do the stuff we need to do 
 set the flag that we'll check again next time and skip this code next time
}

我要做的是检查应用程序是否在
didFinishLaunchingWithOptions:
方法中首次启动,并将应用程序的当前版本存储在默认值中。 这样做的目的是在应用程序的每次更新时,我都可以跟踪需要删除哪些文件,以及需要为该版本的应用程序存储哪些文件。 代码如下:

if (![[NSUserDefaults standardUserDefaults] boolForKey:kNOT_FIRST_LAUNCH]) {
    NSLog(@"fresh install = %d", (int)[self checkForFreshInstall]);
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kAPPLICATION_LAUNCHING_FIRST_TIME];
    [[NSUserDefaults standardUserDefaults] synchronize];
} else {
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:kAPPLICATION_LAUNCHING_FIRST_TIME];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
这是CheckForFreshIntall方法:

- (BOOL) checkForFreshInstall {
NSString *currentVersion = (NSString*)[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
NSString *prevVersion = (NSString *)[[NSUserDefaults standardUserDefaults] valueForKey:@"prevVersion"];

if (prevVersion == nil) {
    // Starting up for first time with NO pre-existing installs (e.g., fresh 
    // install of some version)
    [[NSUserDefaults standardUserDefaults] setValue:currentVersion forKey:@"prevVersion"];
    // Save changes to disk
    [[NSUserDefaults standardUserDefaults] synchronize];
    return YES;
}
else if ([prevVersion intValue] < [currentVersion intValue]) {
    // Starting up for first time with this version of the app. This
    // means a different version of the app was alread installed once 
    // and started.
    [[NSUserDefaults standardUserDefaults] setValue:currentVersion forKey:@"prevVersion"];
    // Save changes to disk
    [[NSUserDefaults standardUserDefaults] synchronize];
    return NO;
}
return YES;
-(BOOL)检查freshinstall{
NSString*currentVersion=(NSString*)[[NSBundle mainBundle]objectForInfoDictionaryKey:@“CbundLeverVersion”];
NSString*prevVersion=(NSString*)[[NSUserDefaults standardUserDefaults]valueForKey:@“prevVersion”];
if(prevVersion==nil){
//首次启动时没有预先安装(例如,新安装
//安装(某些版本)
[[NSUserDefaults standardUserDefaults]设置值:currentVersion forKey:@“prevVersion”];
//将更改保存到磁盘
[[NSUserDefaults standardUserDefaults]同步];
返回YES;
}
如果([prevVersion intValue]<[currentVersion intValue]){
//首次使用此版本的应用程序启动。此
//表示已安装一次不同版本的应用程序
//然后开始。
[[NSUserDefaults standardUserDefaults]设置值:currentVersion forKey:@“prevVersion”];
//将更改保存到磁盘
[[NSUserDefaults standardUserDefaults]同步];
返回否;
}
返回YES;
}


如果您有任何问题,请告诉我。

我要做的是检查应用程序是否已在
didFinishLaunchingWithOptions:
方法中首次启动,并以默认值存储应用程序的当前版本。 这样做的目的是在应用程序的每次更新时,我都可以跟踪需要删除哪些文件,以及需要为该版本的应用程序存储哪些文件。 代码如下:

if (![[NSUserDefaults standardUserDefaults] boolForKey:kNOT_FIRST_LAUNCH]) {
    NSLog(@"fresh install = %d", (int)[self checkForFreshInstall]);
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kAPPLICATION_LAUNCHING_FIRST_TIME];
    [[NSUserDefaults standardUserDefaults] synchronize];
} else {
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:kAPPLICATION_LAUNCHING_FIRST_TIME];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
这是CheckForFreshIntall方法:

- (BOOL) checkForFreshInstall {
NSString *currentVersion = (NSString*)[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
NSString *prevVersion = (NSString *)[[NSUserDefaults standardUserDefaults] valueForKey:@"prevVersion"];

if (prevVersion == nil) {
    // Starting up for first time with NO pre-existing installs (e.g., fresh 
    // install of some version)
    [[NSUserDefaults standardUserDefaults] setValue:currentVersion forKey:@"prevVersion"];
    // Save changes to disk
    [[NSUserDefaults standardUserDefaults] synchronize];
    return YES;
}
else if ([prevVersion intValue] < [currentVersion intValue]) {
    // Starting up for first time with this version of the app. This
    // means a different version of the app was alread installed once 
    // and started.
    [[NSUserDefaults standardUserDefaults] setValue:currentVersion forKey:@"prevVersion"];
    // Save changes to disk
    [[NSUserDefaults standardUserDefaults] synchronize];
    return NO;
}
return YES;
-(BOOL)检查freshinstall{
NSString*currentVersion=(NSString*)[[NSBundle mainBundle]objectForInfoDictionaryKey:@“CbundLeverVersion”];
NSString*prevVersion=(NSString*)[[NSUserDefaults standardUserDefaults]valueForKey:@“prevVersion”];
if(prevVersion==nil){
//首次启动时没有预先安装(例如,新安装
//安装(某些版本)
[[NSUserDefaults standardUserDefaults]设置值:currentVersion forKey:@“prevVersion”];
//将更改保存到磁盘
[[NSUserDefaults standardUserDefaults]同步];
返回YES;
}
如果([prevVersion intValue]<[currentVersion intValue]){
//首次使用此版本的应用程序启动。此
//表示已安装一次不同版本的应用程序
//然后开始。
[[NSUserDefaults standardUserDefaults]设置值:currentVersion forKey:@“prevVersion”];
//将更改保存到磁盘
[[NSUserDefaults standardUserDefaults]同步];
返回否;
}
返回YES;
}


如果您有任何问题,请告诉我。

您可以随时执行类似于
NSArray*currentparts=[currentVersion Components由字符串分隔:@“”
NSArray*perviousParts=[pervVersion Components由字符串分隔:@]然后比较各个部件,以确定安装的版本是否大于以前的版本。希望这有帮助。您可以始终使用两个整数作为主要版本和次要版本(或三个整数作为#.#.#.#)。您可以始终执行类似于
NSArray*currentparts=[currentVersion componentsSeparatedByString:@.”的操作
NSArray*perviousParts=[pervVersion Components由字符串分隔:@]然后比较各个部件,以确定安装的版本是否大于以前的版本。希望这有帮助。您可以始终使用两个整数作为主版本和次版本(或三个整数作为#.#)