我在我的应用程序登录和注销过程中遇到了很多后台计时器服务过程的问题。下面的代码是我在 applicationDidBecomeActive 时为通知和 JSON 服务后台维护的。
-(void)applicationDidBecomeActive:(UIApplication *)application
{
checkJSONtimer = [NSTimer scheduledTimerWithTimeInterval:300 target:self selector:@selector(updateJSON) userInfo:nil repeats:TRUE];
NSLog(@"%s", __PRETTY_FUNCTION__);
application.applicationIconBadgeNumber = 0;
}
下面的问题
- 登录前,计时器事件开始
- 注销后,计时器事件不会停止
- 如果我在后台进入事件状态后评论上面的计时器, 然后定时器不工作
- 每次没有
logout
,清除background app
再次运行 打开我可以从login
仅供引用:
上面的计时器选择器方法我在下面三个地方维护
1. application didFinishLaunchingWithOptions
2. applicationDidBecomeActive
3. applicationDidEnterBackground
如何解决以上问题。请帮助我!
最佳答案
- 登录前,计时器事件开始
For that you have to make a check whether the user is log-in the app or not.For example
-(void)applicationDidBecomeActive:(UIApplication *)application{ if(isUserLogedIn) { checkJSONtimer = [NSTimer scheduledTimerWithTimeInterval:300 target:self selector:@selector(updateJSON) userInfo:nil repeats:TRUE]; NSLog(@"%s", __PRETTY_FUNCTION__); application.applicationIconBadgeNumber = 0; } }
- 注销后,计时器事件不会停止
For that, make a Notification which will trigger when the user log-out from the app.When the notification triggers the invalidate the timer.[myTimer inValidate].
如果我在后台进入事件状态后在上面评论定时器,那么定时器不工作
No need to comment the timer.Just manage the timer affectively.
每次无需注销,清除后台应用程序运行再次打开我可以从登录。
For that you have to manage the User session in NSUserDefaults.Check the value, if the value is not null, the log-in user automatically.
关于ios - 如何使用 objective-c 停止和启动应用程序委托(delegate)计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32092942/