2012-07-10 55 views
0

在我的应用程序中,我有一个函数updatetimer不断更新计时器。我希望此过程在应用程序处于后台时保持继续。但该方法仅在应用程序处于前台时才会调用。在应用程序处于后台时不会调用该方法.Below是那个代码。我该如何解决这个问题?如何在iphone中使用背景应用程序时运行重复过程?

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 


         timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES]; 
         [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; 

         [[NSRunLoop currentRunLoop] run]; 
        }); 
+0

您不必在用户的设备上唯一的应用程序。你必须分享(见下面的答案)。 – borrrden 2012-07-10 06:27:29

回答

3

从苹果文档,只有这些任务被允许在后台运行,而他们每个人都有自己的局限性:

音频的应用程序播放音频内容给用户,而在背景。 (此内容包括使用AirPlay播放音频或视频内容。)

位置 - 该应用程序即使在后台运行时也能让用户了解其位置。

voip-该应用程序提供了用户使用互联网连接拨打电话 的功能。

报刊亭内容该应用程序是一个报亭应用程序,下载和 在后台处理杂志或报纸内容。

外部附件 - 该应用程序与需要 的硬件附件一起通过外部附件框架定期传递更新。

蓝牙中央的应用支持蓝牙配件,需要 通过CoreBluetooth 框架兑现定期更新。

否则你可以要求长达10分钟。额外的运行时间是:(也来自苹果文档)

bgTask = [application beginBackgroundTaskWithExpirationHandler:^{ 
     // Clean up any unfinished task business by marking where you. 
     // stopped or ending the task outright. 
     [application endBackgroundTask:bgTask]; 
     bgTask = UIBackgroundTaskInvalid; 
    }]; 

    // Start the long-running task and return immediately. 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

     // Do the work associated with the task, preferably in chunks. 

     [application endBackgroundTask:bgTask]; 
     bgTask = UIBackgroundTaskInvalid; 
    }); 

了解更多信息: Apple Docs

相关问题