[英]How to stop and start app delegate timer using objective c?
I have in my app login and logout process here I am facing so many problem with background timer services process. 我在我的应用程序登录和注销过程中我在后台计时器服务过程中遇到了很多问题。 Below code I am maintaining for notification and JSON service background while applicationDidBecomeActive. 下面的代码我在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;
}
Problems below 问题如下
logout
, cleared background app
running again open I can go from login
每次没有logout
,清除background app
再次运行打开我可以从login
FYI :
Above timer selector method I am maintaining below three places FYI :
以上计时器选择器方法我保持在三个以下的位置
1. application didFinishLaunchingWithOptions
2. applicationDidBecomeActive
3. applicationDidEnterBackground
How to solve above problems. 如何解决上述问题。 Please help me! 请帮我!
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]. 为此,制作一个通知,当用户从应用程序注销时将触发通知。当通知触发计时器无效时。[myTimer inValidate]。
If I commented above timer after background to come active state, then timer not working 如果我在后面的定时器上注释到后台进入活动状态,那么定时器不工作
No need to comment the timer.Just manage the timer affectively. 无需评论计时器。只需有效管理计时器。
Every time without logout , cleared background app running again open I can go from login. 每次没有注销,清除后台应用程序再次运行打开我可以从登录。
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. 为此,您必须在NSUserDefault中管理用户会话。如果值不为空,请检查该值,自动登录用户。
If you want to cancel the timer you can use something like this: 如果你想取消计时器你可以使用这样的东西:
AppDelegate* appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[appDelegate.checkJSONtimer invalidate]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.