如何在iphone上首次确定负载应用?

时间:2010-11-11 10:35:28

标签: iphone objective-c cocoa

我希望第一次有人加载我的应用程序,让它从我拥有的首选项视图开始,每隔一段时间从主视图开始。

我找不到一种方法来检测这是否是第一次运行应用程序。有任何想法吗?

- (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;
}

这里Red& Blue是两个类中uiviewcontroller的子类,只有一个不同之处在于 - (void)viewDidLoad { self.view.backgroundcolor = [UIColor redColor]; 在Red class&在显示蓝色backgroundcolor的蓝色类 但是,当我执行应用程序时,它的显示只有蓝色不显示红色,我错了我做的时候,当我运行应用程序时,它显示红色.....

4 个答案:

答案 0 :(得分:18)

以下是如何做到的。您会很高兴知道这非常容易。这正是四行代码。

将此代码添加到您想要的任何位置。也许只是在您的应用程序中:didFinishLaunchingWithOptions:文件AppDelegate.m中的例程。或者,无论您在何处为应用程序进行常规设置。 (但是,请确保它仅运行一次。)

NSUserDefaults      *padFactoids;
int                 launchCount;

padFactoids = [NSUserDefaults standardUserDefaults];
launchCount = [padFactoids integerForKey:@"launchCount" ] + 1;
[padFactoids setInteger:launchCount forKey:@"launchCount"];
[padFactoids synchronize];

NSLog(@"number of times: %i this app has been launched", launchCount);

if ( launchCount == 1 )
    {
    NSLog(@"this is the FIRST LAUNCH of the app");
    // do stuff here as you wish
    }
if ( launchCount == 2 )
    {
    NSLog(@"this is the SECOND launch of the damn app");
    // do stuff here as you wish
    }

// enjoy!

除了最简单的应用程序之外,几乎每个应用都会这样做。希望能帮助到你。从理论上来说,你不一定要打扰“同步”调用,但是我们已经发现了大量的实际用户运行,如果你把它包括在内,它可能更可靠。

PS 不要在首选项中使用布尔值。 如果您是新程序员,那么理解默认值并因此无法维护是不明智的。坚持整数。 (首次使用时,它们总是“整数零”,因此您没有问题。)Easy Peasy。希望它有所帮助。

答案 1 :(得分:1)

检查NSUserdefaults中的一些标志,如果不存在,则显示首选项视图,并在设置这些设置后设置该标志。

BOOL hasBeenStarted = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasBeenStarted"];
if (!hasBeenStarted) {
    NSLog(@"Show panel");
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasBeenStarted"];
}

答案 2 :(得分:0)

如果您在应用启动时创建local file,情况会更好。并在每次发布时检查文件是否存在。

如果是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);
    }
}

答案 3 :(得分:0)

BOOL hasBeenStarted = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasBeenStarted"];
if (!hasBeenStarted) {
    NSLog(@"Show panel");
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasBeenStarted"];
    [[NSUserDefaults standardUserDefaults]synchronize];// save to NSUserDefaults

}