【问题标题】:iOS app Launch DetectioniOS 应用启动检测
【发布时间】:2015-03-02 07:23:14
【问题描述】:

我正在尝试实现我的应用,以便在每 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
                    }

【问题讨论】:

    标签: objective-c loops detect launch


    【解决方案1】:

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

    替换您的 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

    【讨论】:

    • 非常感谢。这正是我想要的。
    猜你喜欢
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 2010-09-23
    相关资源
    最近更新 更多