2012-02-22 100 views
0

我正在使用我的应用程序中的iphone后台进程。它在后台运行,但在10分钟后它终止了我的应用程序。而不是长时间工作。iphone后台进程在我的应用程序中长时间不工作

我的应用程序包含蒂莫1个计数器在那时我的计数器开始,当我会点击home键就会在后台运行viewDidLoad中。在后台运行完美,但10分钟后,它停止了我的后台进程。以下是我的代码。

in .h文件。

IBOutlet UILabel *thecount; 
int count; 
NSTimer *theTimer; 
UIBackgroundTaskIdentifier counterTask; 
在我的.m

- (void)viewDidLoad { 

    UIBackgroundTaskIdentifier bgTask = nil; 
    UIApplication *app = [UIApplication sharedApplication]; 


    counterTask = [[UIApplication sharedApplication] 
        beginBackgroundTaskWithExpirationHandler:^{ 
         [app endBackgroundTask:counterTask]; 
         //counterTask = UIBackgroundTaskInvalid; 

         // If you're worried about exceeding 10 minutes, handle it here 
         theTimer=[NSTimer scheduledTimerWithTimeInterval:0.5 
                   target:self 
                   selector:@selector(countUp) 
                   userInfo:nil 
                   repeats:YES]; 
        }]; 

    count=0; 
    theTimer=[NSTimer scheduledTimerWithTimeInterval:0.5 
         target:self 
         selector:@selector(countUp) 
         userInfo:nil 
         repeats:YES]; 

    [super viewDidLoad]; 
} 
  • (无效)COUNTUP {

    count++; 
    NSString *currentCount; 
    
    currentCount=[[NSString alloc] initWithFormat:@"%d",count]; 
    NSLog(@"timer running in background :%@",currentCount); 
    thecount.text=currentCount; 
    [currentCount release]; 
    

}

上面的代码只是采样计数数eground和背景。但在10分钟后它不起作用。

所以请帮助我,并为我提供一些帮助。

+1

从更多类型的谷歌搜索我发现如何实施后台进程,它将适用于所有ios,除了ios 3.0 .. http://www.mindsizzlers.com/2011/07/ios-background-location/ – 2012-11-09 10:21:37

回答

4

10分钟是长时间运行的后台任务的最大时间。 iOS不会推广长时间运行的后台进程,为了您的用户,您不应该尝试去做。

为了让你的代码比这更长的运行,其他一些外部事件可能使你的应用程序被再次唤醒,或者持续运转,例如GPS定位事件,在您的VoIP型应用程序中播放音频或传入VoIP连接。

+1

感谢您的回复。在我的应用程序中,我在某些时候发送GPS数据给服务器。以上只是代码在后台工作。请你提供一些详细的示例,以便如何在某些时候将gps数据发送到我的服务器,并运行应用程序。所以我可以理解它。 – 2012-02-22 10:33:54

+0

因此,只要您收到设备位置的通知,就可以安排任务将该位置发送到服务器。你可以在CLLocationManagerDelegate协议实现类的'locationManager:didUpdateToLocation:fromLocation'方法中做到这一点。 – macbirdie 2012-02-22 11:02:10

+0

ohh,你没有得到我的观点,我在某些特定的时差跟踪用户在后台模式。所以我必须像后台任务一样实施。 [我想实现像这样](http://stackoverflow.com/questions/5323634/app-running-in-background)但我不能正确理解答案。想引导我? – 2012-02-22 11:05:59

相关问题