我正在尝试在后台线程中运行进程。我希望这个过程在60秒内完成,然后重新开始运行。无论应用程序是在前台还是后台。我不知道如何实施,在哪里实施。我用的是ios7。在这个过程中,我也接受位置更新。
我读了关于后台任务的信息,但它没有给出正确的过程概念。有人能为我提供好的来源或链接吗?
ios没有为后台进程提供这样的api,不像android使用service。你可以使用定时器来处理连续的后台进程。在后台中还有dispatch_async,selector来进行高效的后台处理。
你可以使用这样的东西来进行后台处理,但是要记住苹果有10-15分钟的限制来完成处理。
UIApplication* app = [UIApplication sharedApplication];
task = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:task];
task = 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.
NSLog(@"Started background task timeremaining = %f", [app backgroundTimeRemaining]);
if (connectedToNetwork) {
// do work son...
}
[app endBackgroundTask:task];
task = UIBackgroundTaskInvalid;
});
您还可以检查以下内容:
**BOOL backgroundSupported = NO;
if ([device respondsToSelector:@selector(isMultitaskingSupported)])
backgroundSupported = device.multitaskingSupported;**