开发者

iPhone - NSTimer problem when iphone locks

开发者 https://www.devze.com 2023-03-31 15:14 出处:网络
I am having NSTimer that check for latest data from server every 15 seconds. When the app goes to background, i am stopping the timer in following app delegate method

I am having NSTimer that check for latest data from server every 15 seconds. When the app goes to background, i am stopping the timer in following app delegate method

- (void)applicationWillResignActive:(UIApplication *)application

When the app comes back to active, i am activating timers and it becomes active again

- (void)applicationWillEnterForeground:(UIApplication *)application

This functional开发者_JAVA百科ity works fine. But when I leave the iPhone, it locks after some time. When I unlock it, the functionality is not continuing properly. Timer is not running.

What am I doing wrong?

I don't want timers to run in background. That's why before going to background, i'm disabling the timer and when comes to foreground, i want to restart timer.


I think you may need an additional delegate method other than applicationWillEnterForeground. I think that when the user unlocks their phone, your app will already be in the foreground and so will not enter it. But I'm not sure what the delegate method will be, maybe applicationDidBecomeActive


That is just how the Apple background processing works...

You can create a background task which will make the NSTimer's work in the background BUT they are limited in time, you can't have it working always in the background.

0

精彩评论

暂无评论...
验证码 换一张
取 消