在后台运行iOS App超过10分钟

我试图让我的应用程序在后台运行10分钟以上,根据这个和我的好以下。 (我想使用长时间的背景运行来跟踪一个位置,我的代码在这里只是使用计数器进行testing)任何人都可以帮忙指出问题所在?

实施长期运行的后台任务

对于需要更多执行时间来执行的任务,您必须请求特定的权限才能在后台运行它们,而不会被暂停。 在iOS中,只有特定的应用程序types才能在后台运行:

在后台播放可听内容的应用,例如音乐播放器应用

随时向用户通知其位置的应用,例如导航应用

支持网际协议语音(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]; } } 

所以你的应用使用位置服务。 然后请阅读位置感知编程指南 。

您需要对Info.plist进行一些更改:

  • 如果您的应用程序依赖位置服务正常工作,请将location-services添加到UIRequiredDeviceCapabilities
  • 如果您的应用需要GPS硬件,请将gps添加到UIRequiredDeviceCapabilities
  • 如果您需要在后台运行您的应用程序超过10分钟,请将location添加到UIBackgroundModes 然后,您的位置经理将在超过10分钟的限制内提供地点信息。
  • 你还应该设置NSLocationUsageDescription (也可以是本地化的)

在后台获取位置事件

如果您的应用程序需要位置更新,无论该应用程序处于前台还是后台,都有多种select。 首选方法是使用重要位置更改服务在适当的时间唤醒您的应用程序以处理新事件。 但是,如果您的应用需要使用标准位置服务,则可以将您的应用声明为需要后台位置服务。

只有在没有这些服务会削弱其运营能力的情况下,应用程序才会请求后台定位服务。 此外,任何请求后台定位服务的应用程序都应该使用这些服务为用户提供切实的好处。 例如,一个转弯的导航应用程序可能是后台定位服务的候选人,因为它需要跟踪用户的位置,并在下次轮到时进行报告。

请参阅phix23的答案(和文档)的细节,但在这里我想解释你可以期望发生的事情。

这在您引用的文档中已经涵盖了很多。

任何应用程序都可以在后台运行长达十分钟。 这就是beginBackgroundTaskWithExpirationHandler:方法的作用。 无论您设置了哪些标志和选项,只要使用该方法即可。

对于需要跟踪位置的应用程序,您可以使用CLLocationManager 。 这不允许你的应用程序在后台运行,只要你喜欢。 相反,当有趣的事发生时,它会通知你 – 这就是代表的意思。 所以你不能保证你的countUp方法每10分钟被调用一次,但是当用户把手机移动一定距离时你可以让OS调用它。

通过在applicationDidEnterBackground方法中添加以下内容,似乎可以永久执行:

 [app beginBackgroundTaskWithExpirationHandler:^{}]; 

然后,您将使willEnterForeground的长任务无效。

我最近在iOS 6上成功了,但我不确定它会被批准用于商店。

顺便说一下,在你的plist中,你可以设置Required background modes ,然后在Item0 App registers for location updates