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

iOS*_*ser 3 iphone cocoa objective-c

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

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

- (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;
}
Run Code Online (Sandbox Code Playgroud)

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

Fat*_*tie 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!
Run Code Online (Sandbox Code Playgroud)

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

PS 不要在偏好中使用布尔值.如果您是一名新程序员,那么理解默认值并因此无法维护是不明智的.坚持整数.(首次使用时,它们总是"整数零",因此您没有问题.)Easy Peasy.希望能帮助到你.