简体   繁体   English

iOS后台模式和NStimer

[英]iOS background mode and NStimer

I'm creating an app which works as a boxing timer, counting rounds and playing a sound at the end of each. 我正在创建一个用作拳击计时器的应用程序,计算回合数并在每个回合末尾播放声音。 The problem is that NSTimer stops when the app enters background mode. 问题是当应用程序进入后台模式时,NSTimer会停止。 There are some boxing timers in the app store, so i'm shore there is some way that can be implemented. 应用程序商店中有一些装箱计时器,所以我支持采用某种方法。

You should store current time before going to background ( In your AppDelegate ) : 您应该在进入后台之前存储当前时间(在AppDelegate ):

- (void)applicationWillResignActive:(UIApplication *)application
{
     self.startTime = [NSDate date];
    .
    .
    .
    // Rest of your code
}

And then do the same when app goes active : 然后在应用程序激活时执行相同的操作:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    self.endTime = [NSDate date];
    .
    .
    .
    // Rest of your code
}

Then you can subtract them and find out how much the app was in the background and do what you want based on that. 然后,您可以减去它们,以找出应用程序在后台的数量,然后根据该应用程序执行所需的操作。

If you want to do something when your app is in the background, you should enable Background Mode in your app's .plist file ( or in Capabilities Section ), which usually is used for VOIP apps or something similar. 如果您想在应用程序处于后台运行时执行某些操作,则应在应用程序的.plist文件(或“ Capabilities部分)中启用“ Background Mode ,该模式通常用于VOIP应用程序或类似的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM