我试图让我的应用程序在后台运行超过10分钟,根据这一点和我的好下面。(我想使用长背景运行来跟踪位置,这里的代码只是为了测试目的而使用计数器)任何人都可以帮助指出问题所在吗?
实现长期运行的后台任务 对于需要更多执行时间来实现的任务,必须请求特定权限才能在后台运行这些任务,而不暂停这些权限。在iOS中,只允许特定的应用程序类型在后台运行: 在后台向用户播放可听内容的应用程序,例如音乐播放器应用程序。 随时通知用户位置的应用程序,例如导航应用程序。 支持互联网语音协议(VoIP)的应用程序 报摊应用程序,需要下载和处理从外部附件定期更新的新内容应用程序 实现这些服务的应用程序必须声明它们支持的服务,并使用系统框架来实现这些服务的相关方面。声明服务可以让系统知道您使用的是哪些服务,但在某些情况下,实际上是系统框架阻止您的应用程序被挂起。
- (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]; } }发布于 2013-04-16 08:10:01
所以你的应用程序使用定位服务。那么请阅读位置意识编程指南。
您需要对您的Info.plist进行一些更改:
location-services添加到UIRequiredDeviceCapabilities中。gps添加到UIRequiredDeviceCapabilities中。location UIBackgroundModes**.中添加。那么您的位置经理将提供超过10分钟限制的位置。**NSLocationUsageDescription (也可以本地化)在后台获取位置事件 如果您的应用程序需要提供位置更新,无论应用程序在前台还是后台,都有多个选项可供选择。首选的选择是在适当的时候使用重要的位置更改服务唤醒应用程序来处理新事件。但是,如果您的应用程序需要使用标准位置服务,您可以声明您的应用程序需要后台位置服务。 只有当没有这些服务会损害其操作能力时,应用程序才应请求后台位置服务。此外,任何请求后台位置服务的应用程序都应该使用这些服务为用户提供实实在在的好处。例如,一个逐转导航应用程序可能会成为后台定位服务的候选应用,因为它需要跟踪用户的位置,并在下次转弯时进行报告。
发布于 2013-04-16 08:15:50
有关细节,请参阅phix23的答案(和文档),但在这里,我想解释一下您可以预期会发生什么。
您引用的文档中几乎涵盖了这些内容。
任何应用程序都可以在后台运行10分钟。这就是beginBackgroundTaskWithExpirationHandler:方法所做的。不管您设置了哪些标志和选项,使用该方法都会得到这些信息。
对于需要跟踪位置的应用程序,您可以使用CLLocationManager。这不允许你的应用程序在后台运行,只要你愿意。相反,当发生有趣的事情时,它会通知您--这就是委托的目的。因此,您不能保证您的countUp方法每十分钟被调用一次,但是当用户移动一定距离的手机时,您可以让操作系统调用它。
发布于 2013-08-21 13:29:45
通过在applicationDidEnterBackground方法中添加以下内容,它似乎可以永远执行:
[app beginBackgroundTaskWithExpirationHandler:^{}];然后使willEnterForeground中的长任务失效。
我最近在iOS 6上取得了成功,但我不确定它是否会获得商店的批准。
https://stackoverflow.com/questions/16031120
复制相似问题