【发布时间】:2012-02-22 09:58:33
【问题描述】:
我正在我的应用程序中使用 iphone 后台进程。它在后台运行,但 10 分钟后它正在终止我的应用程序。并且长时间不工作。
当 viewdidload 时,我的应用程序包含 1 个带有 timmer 的计数器,那时我的计数器将启动,当我单击主页按钮时,它将在后台运行。在后台它运行完美,但 10 分钟后它正在停止我的后台进程。以下是我的代码。
在 .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];
}
-
(void)countUp {
count++; NSString *currentCount; currentCount=[[NSString alloc] initWithFormat:@"%d",count]; NSLog(@"timer running in background :%@",currentCount); thecount.text=currentCount; [currentCount release];
}
上面的代码只是示例来计算前景和背景的数量。但 10 分钟后它不起作用。
所以请帮助我并为此提供一些帮助。
【问题讨论】:
-
从更友好的谷歌搜索中我发现了如何实现后台进程,它适用于除 ios 3.0 之外的所有 ios..mindsizzlers.com/2011/07/ios-background-location
标签: iphone background