ios - 如何使用 objective-c 停止和启动应用程序委托(delegate)计时器?

标签 ios objective-c timer

我在我的应用程序登录和注销过程中遇到了很多后台计时器服务过程的问题。下面的代码是我在 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;
}

下面的问题

  1. 登录前,计时器事件开始
  2. 注销后,计时器事件不会停止
  3. 如果我在后台进入事件状态后评论上面的计时器, 然后定时器不工作
  4. 每次没有logout,清除background app再次运行 打开我可以从login

仅供引用:上面的计时器选择器方法我在下面三个地方维护

1. application didFinishLaunchingWithOptions
2. applicationDidBecomeActive
3. applicationDidEnterBackground

如何解决以上问题。请帮助我!

最佳答案

  1. 登录前,计时器事件开始

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;
   }
}
  1. 注销后,计时器事件不会停止

    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].

  2. 如果我在后台进入事件状态后在上面评论定时器,那么定时器不工作

    No need to comment the timer.Just manage the timer affectively.

  3. 每次无需注销,清除后台应用程序运行再次打开我可以从登录。

    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/

相关文章:

ios - 无法从 bool 条件显示 UILabel

javascript - 如何在 0 :0 处停止 JavaScript 计时器

android - React Native 中 AsyncStorage 的键的命名约定是什么?

ruby-on-rails - SHA1 哈希值在我的 Rails 和 Cocoa 应用程序之间不匹配

ios - 将单元格放在表格 View 的特定部分

iphone - 如何在应用程序首次启动时显示 UIAlertView(或 UIView)?

Java 长时间运行的计时器正在被 GC 使用

java - 获取精确时间间隔的 GPS 位置

ios - 在不叠加的情况下以随机持续时间生成对象

ios - 如何从 NSDictionary (iOS) 中的键/值对中提取值