NSTimer Keep counting after app is closed in iPhone

你离开我真会死。 提交于 2019-12-01 10:36:33

You can't - once your app is closed then it's not running anymore so the timer won't be running either.

Take a look at local notifications?

When application goes in background, In –(void)applicationDidEnterBackground: application delegate method add current counter value and current time in nsuserdefault.

Now when application becomes active before that –(void)applicationWillEnterForeground: will called so in that method get total seconds application was in background ie (current time of application) - (time when application went background which is stored in nsuserdefault) calculate in seconds

so add this also in –(void)applicationWillEnterForeground :

 if((seconds calculated) > (28800 - (current counter value stored in nsuserdefault)))
 {
    // stop timer as it has gone beyond eight hours
 } 
 else
 {
  // continue task
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!