我试图让我的应用程序在后台运行超过10分钟,根据这个和我的好下面。(我想使用长时间后台运行来跟踪位置,我的代码在这里只是简单地使用一个计数器用于测试目的)谁能指出问题是什么?
实现长时间运行的后台任务
对于需要更多执行时间来实现的任务,您必须请求特定的权限以在后台运行它们他们被停职了。在iOS中,只允许特定的应用类型在后台运行:
在后台为用户播放可听内容的应用程序;比如音乐播放器应用
随时通知用户位置的应用程序,例如导航应用
支持VoIP (Voice over Internet Protocol)的应用
需要下载和处理新内容的报摊应用程序接收外部配件的定期更新
实现这些服务的应用程序必须声明它们使用的服务支持和使用系统框架来实现的相关方面这些服务。声明服务让系统知道哪些服务您使用的服务,但在某些情况下是系统框架实际防止你的应用程序被挂起。
- (void)viewDidLoad {
[super viewDidLoad];
counterTask = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:^{
}];
count=0;
theTimer=[NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(countUp)
userInfo:nil
repeats:YES];
}
- (void)countUp {
{
count++;
NSString *currentCount;
currentCount=[[NSString alloc] initWithFormat:@"%ld",count];
theCount.text=currentCount;
[currentCount release];
}
}
另一个问题:我可以让iOS应用程序永远在后台运行吗?
----编辑代码添加位置,仍然不运行超过10分钟,对我做错了什么有帮助吗?
- (void)viewDidLoad {
[super viewDidLoad];
count=0;
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
counterTask = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:^{
// If you're worried about exceeding 10 minutes, handle it here
[locationManager startUpdatingLocation];
}];
theTimer=[NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(countUp)
userInfo:nil
repeats:YES];
}
(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"OldLocation %f %f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude); NSLog(@"NewLocation %f %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
count++; NSString *currentCount;
currentCount=[[NSString alloc] initWithFormat:@"%ld",count];
theCount.text=currentCount; [currentCount release];
}
(void)countUp { [locationManager startUpdatingLocation];
{ count++; NSString *currentCount;
currentCount=[[NSString alloc] initWithFormat:@"%ld",count];
theCount.text=currentCount;
[currentCount release]; } }
所以你的应用使用位置服务。那么请阅读位置感知编程指南。
你需要对Info.plist做一些修改:
- 如果你的应用程序依赖于位置服务来正常工作,将
location-services
添加到UIRequiredDeviceCapabilities
- 如果你的应用需要GPS硬件,将
gps
添加到UIRequiredDeviceCapabilities
-
如果你需要在后台运行你的应用程序超过10分钟,将
location
添加到UIBackgroundModes
。然后您的位置管理器将提供超过10分钟限制的位置。 - 你也应该设置
NSLocationUsageDescription
(也可以本地化)
在后台获取位置事件
如果你的应用需要位置更新,无论它是否在前台或后台,有多种选择。的首选方案是使用显著位置更改服务在适当的时间唤醒应用程序来处理新事件。然而,如果你的应用需要使用标准的位置服务,你可以声明你的应用需要后台定位服务
一个应用程序应该请求后台定位服务,只有当缺席这些服务将损害其运作能力。此外,任何请求后台位置服务的应用都应该使用这些服务为用户提供切实的利益。例如,a逐向导航应用程序可能是背景的候选人定位服务,因为它需要跟踪用户的位置和何时该转弯,请报告。
请参阅phix23的答案(和文档)了解详细信息,但在这里我想解释您可能会发生的事情。
这些在你引用的文档中都有详细的介绍。
任何应用程序都可以在后台运行长达十分钟。这就是beginBackgroundTaskWithExpirationHandler:
方法所做的。不管你设置了哪些标志和选项,那都是你使用该方法所能得到的。
对于需要跟踪位置的应用程序,可以使用CLLocationManager
。这不允许你的应用程序在后台运行,只要你喜欢。相反,它会在有趣的事情发生时通知你,这就是委托的作用。所以你不能保证你的countUp
方法每十分钟被调用一次,但是你可以让操作系统在用户移动手机一定距离时调用它。
通过在applicationDidEnterBackground
方法中添加以下内容,它似乎可以永远执行:
[app beginBackgroundTaskWithExpirationHandler:^{}];
然后在willEnterForeground
中使长任务无效。
我最近在iOS 6上取得了成功,但我不确定它是否会被批准进入商店。
顺便说一下,你也可以在列表中设置Required background modes
,然后在Item0中设置App registers for location updates