如何确定首次在iphone上加载应用程序?
我希望第一次有人加载我的应用程序时,它会在我的首选项视图中启动,每隔一段时间就会在主视图中启动 我找不到一种方法来检测这是否是应用程序第一次运行。有什么想法吗如何确定首次在iphone上加载应用程序?,iphone,objective-c,cocoa,Iphone,Objective C,Cocoa,我希望第一次有人加载我的应用程序时,它会在我的首选项视图中启动,每隔一段时间就会在主视图中启动 我找不到一种方法来检测这是否是应用程序第一次运行。有什么想法吗 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after applicat
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch
NSUserDefaults *padFactoids;
int launchCount;
padFactoids = [NSUserDefaults standardUserDefaults];
launchCount = [padFactoids integerForKey:@"launchCount" ] + 1;
[padFactoids synchronize];
NSLog(@"this is the number: %i of times this app has been launched", launchCount);
if ( launchCount == 1 )
{
NSLog(@"this is the FIRST LAUNCH of the app");
// do stuff here as you wish
bbb = [[Blue alloc]init];
[window addSubview:bbb.view];
}
if ( launchCount >= 2 )
{
NSLog(@"this is the SECOND launch of the damn app");
// do stuff here as you wish
rrr = [[Red alloc]init];
[window addSubview:rrr.view];
}
[window makeKeyAndVisible];
return YES;
}
在这两个类中,红色和蓝色都是uiviewcontroller的子类。唯一的区别是-(void)viewDidLoad{
self.view.backgroundcolor=[UIColor redColor];
}如果为红色,则为蓝色,背景色为蓝色
但是,当我执行应用程序时,它只显示蓝色,不显示红色,这是我在运行应用程序时出错的地方。第二次,它显示红色。…检查NSUserdefaults中的一些标志,如果没有,则显示首选项视图,并在设置这些设置后设置该标志
BOOL hasBeenStarted = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasBeenStarted"];
if (!hasBeenStarted) {
NSLog(@"Show panel");
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasBeenStarted"];
}
下面就是具体的操作方法。你会很高兴知道这是难以置信的简单。这正好是四行代码。
将此代码添加到任意位置。也许只是在应用程序中:在AppDelegate.m文件中的didfishlaunchingwithoptions:routine。或者,无论您在哪里为应用程序进行常规设置。(但是,请确保它只运行一次。)
除了最简单的应用程序外,几乎所有的应用程序都能做到这一点。希望能有帮助。从理论上讲,您不必为“synchronize”调用而烦恼,但我们发现,在大量实际用户运行中,如果您将其包括在内,则可能更可靠
PS不要在首选项中使用布尔值。如果您是一名新程序员,则无法理解默认值,因此永远无法维护。坚持整数。(第一次不用时,它们总是一个“整数零”,所以你没有问题。)简单。希望有帮助。如果在应用程序启动时创建一个
本地文件,效果会更好。并在每次启动时检查该文件是否存在
对于NSUserDefault
,如果应用程序未在后台运行,则每次都会重新初始化
- (void)checkApplicationFirstLaunch
{
if(![self getFileExistence:@"appinstall"])
{
// this is fresh install of the app
[self createFileFromString:@"install" filename:@"appinstall"];
}
else
{
// app was already installed
}
}
下面是我在上面使用的方法
- (BOOL) getFileExistence: (NSString *) filename
{
BOOL IsFileExists = NO;
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *favsFilePath = [documentsDir stringByAppendingPathComponent:filename];
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the database has already been created in the users filesystem
if ([fileManager fileExistsAtPath:favsFilePath])
{
IsFileExists = YES;
}
return IsFileExists;
}
- (void)createFileFromString:(NSString *)string filename:(NSString *)filename
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
NSLog(@"%@", documentsDirectory);
NSError *error;
BOOL succeed = [string writeToFile:[documentsDirectory stringByAppendingPathComponent:filename] atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (!succeed){
// Handle error here
NSLog(@"Did not save: Error: %@", error);
}
}
请详细解释
- (BOOL) getFileExistence: (NSString *) filename
{
BOOL IsFileExists = NO;
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *favsFilePath = [documentsDir stringByAppendingPathComponent:filename];
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the database has already been created in the users filesystem
if ([fileManager fileExistsAtPath:favsFilePath])
{
IsFileExists = YES;
}
return IsFileExists;
}
- (void)createFileFromString:(NSString *)string filename:(NSString *)filename
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
NSLog(@"%@", documentsDirectory);
NSError *error;
BOOL succeed = [string writeToFile:[documentsDirectory stringByAppendingPathComponent:filename] atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (!succeed){
// Handle error here
NSLog(@"Did not save: Error: %@", error);
}
}
BOOL hasBeenStarted = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasBeenStarted"];
if (!hasBeenStarted) {
NSLog(@"Show panel");
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasBeenStarted"];
[[NSUserDefaults standardUserDefaults]synchronize];// save to NSUserDefaults
}