iOS应用程序启动检测

时间:2015-01-04 16:56:14

标签: objective-c loops detect launch

我正在尝试实施我的应用程序,以便在每次第3次发布时执行某些操作。

我想知道是否有更有效的方法来做到这一点,而不是我在下面所做的......

NSUserDefaults *timesRan;
int launchCount;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

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



                    if(launchCount == 3) {
                       //Do something every 3rd launch
                    }

                    if(launchCount == 6) {
                        //Do something every 3rd launch
                    }


                    if(launchCount == 9) {
                        //Do something every 3rd launch
                    }

                    if(launchCount == 12) {
                        //Do something every 3rd launch
                    }

                    if(launchCount == 15) {
                        //Do something every 3rd launch
                    }

1 个答案:

答案 0 :(得分:0)

可能不是更好的解决方案,但仍然是更好的条件检查。

替换if条件
if (launchCount  == 3) // Use launchCount % 3 == 0 if not reseting counters
{
    launchCount = 0 //reset counter and synchronise.
    [timesRan setInteger:launchCount forKey:@"hasRan"];
    [timesRan synchronize];
    // Additional logic goes here to be done on third launch
}

您可能也希望使用applicationWillEnterForeground