在applicationDidEnterBackground中监听服务器

时间:2015-02-15 15:21:06

标签: ios objective-c

我有一个消息传递应用程序,我希望应用程序在后台侦听服务器,当新消息到来时,会触发通知。 我尝试使用计时器和backgroundtasks,我可以监听服务器,应用程序触发通知但是当我重新打开应用程序时,我无法与任何东西进行交互,例如应用程序被锁定。你能建议任何正确的方法吗?

 - (void)applicationDidEnterBackground:(UIApplication *)application
    {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sbCheckServerRecords) name:@"UIApplicationDidEnterBackgroundNotification" object:nil];
    }
    -(void) sbCheckServerRecords{
          self.tmrCheckRecords = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:1] interval:10.0 target:self selector:@selector(sbCheckRecords) userInfo:nil repeats:YES];
          [self.tmrCheckRecords fire];
     }
    -(void) sbCheckRecords{
        @try{
        if(any message comes)
           notify user;
        }
     }

1 个答案:

答案 0 :(得分:0)

我使用了timer作为dispatch_source_t和 这堂课对我有用:

https://gist.github.com/maicki/7622108