如何确定在iphone上的负载应用程序第一次? [英] how to determine load application on iphone at first time?

查看:72
本文介绍了如何确定在iphone上的负载应用程序第一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望第一次任何人加载我的应用程序,让它开始在偏好视图我有每隔一段时间从主视图开始。

I wish the first time anyone loads my application to have it start at the preferences view i have and every other time to start at the main view.

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

I could not find a way to detect if this is the first time the application is run. any ideas?

- (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];
}在红色类&在蓝色类,它显示蓝色backgroundcolor
但是当我执行应用程序,其显示只有蓝色不显示红色,我错了什么我做,当我跑的应用程序IInd时间它显示红色.....

here Red & Blue are subclasses of uiviewcontroller in both classes only one difference is that in -(void)viewDidLoad{ self.view.backgroundcolor = [UIColor redColor]; }in case of Red class & in blue class which shows blue backgroundcolor but when i execute app its shows only blue color not show red color where i wrong what i do for when i ran app IInd time it shows red color.....

推荐答案

这里是如何做到的。你会很高兴知道这是令人难以置信的容易。它只有四行代码。

Here is exactly how to do it. You will be pleased to know it is incredibly easy. It is exactly FOUR lines of code.

将此代码添加到任何您想要的地方。也许只是在你的应用程序:didFinishLaunchingWithOptions:routine在文件AppDelegate.m。或者,无论你在哪里为你的应用程序做一般设置。 (但是,请确保它只会运行一次

Add this code anywhere you want. Perhaps simply in your application:didFinishLaunchingWithOptions: routine in the file AppDelegate.m. Or, wherever you do general setup for your application. (However, be sure it will run once only.)

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!

几乎每个应用程序,除了最简单的,希望它有帮助。对于理论上的记录,你不一定要打扰同步调用,但我们发现大量的真实用户运行它可能更可靠,如果你包括它。

Almost every app, other than the simplest, does this. Hope it helps. For the record in theory you do not necessarily have to bother with the "synchronize" call but we have found over huge numbers of real-life user runs it is probably more reliable if you do include it.

PS 不要在偏好设置中使用布尔值。 如果你是一个新程序员,它是iffy了解默认值,坚持整数。 (他们总是一个整数零,当第一次未使用,所以你没有问题。)易Peasy。希望它有帮助。

PS Do NOT use Booleans in preferences. If you are a new programmer, it is iffy to understand the defaults and hence never maintainable. Stick to integers. (They are always an "integer zero" when first unused, so you have no problems.) Easy Peasy. Hope it helps.

这篇关于如何确定在iphone上的负载应用程序第一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆