2

NSUserDefault を使用してアプリケーションが初めて開かれたかどうかを検出する方法を知っています。

BOOL didRunBefore = [[NSUserDefaults standardUserDefaults] boolForKey:@"didRunBefore"];

     if (!didRunBefore) {
      //Your Launch Code
      [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"didRunBefore"];
      [[NSUserDefaults standardUserDefaults] synchronize];

       }

問題: どのような機能が含まれているかを説明する Alert for ever ビューが必要で、アプリが最初に起動されたときにのみこのアラートを開きますか?

4

4 に答える 4

10

As an OOP programmer you can make a common public method.

+ (BOOL)checkWhetherRunBefore:(NSString *)key
{
    return [[NSUserDefaults standardUserDefaults] boolForKey:key];
} 

+ (void)hasRunForMyClass:(NSString *)key
{
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

And in your ViewController , you can code in the viewWillAppear or viewDidAppear like this :

- (void)viewWillAppear
{
    if(![HelpController checkWhetherRunBefore:NSStringFromClass([self class])])
    {
        //do your thing
        [HelpController hasRunForMyClass:NSStringFromClass([self class])]
    }

}
于 2013-01-29T03:03:11.787 に答える
1

You just need to put your code in viewDidLoad

viewDidLoad will run only one time when app load your view. (except low memory)

Next time you load the view again in viewDidLoad, you can check bool didRunBefore

于 2013-01-29T02:48:17.440 に答える
1

You can use any key you want.

In FirstViewController:

BOOL didRunBefore = [[NSUserDefaults standardUserDefaults] boolForKey:@"FirstViewController_didRunBefore"];

if (!didRunBefore) {
    //Your Launch Code
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"FirstViewController_didRunBefore"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

In SecondViewController:

BOOL didRunBefore = [[NSUserDefaults standardUserDefaults] boolForKey:@"SecondViewController_didRunBefore"];

if (!didRunBefore) {
    //Your Launch Code
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"SecondViewController_didRunBefore"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
于 2013-01-29T02:49:02.290 に答える
0

アプリケーションの最初の起動を検出する必要があります。このように検出できます

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];  
if (![defaults objectForKey:@"firstRun"])
  [defaults setObject:[NSDate date] forKey:@"firstRun"];

[[NSUserDefaults standardUserDefaults] synchronize];
You can then test for it later...

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];        
if([defaults objectForKey:@"firstRun"])           
{
  // do something or not...
}
于 2013-01-29T02:54:56.053 に答える