[英]Location Background Mode is not working on iOS
I am trying to enable background location mode in my application. 我正在尝试在应用程序中启用后台定位模式。 I have enabled 'Location updates' background mode in my plist file. 我在我的plist文件中启用了“位置更新”后台模式。 The app contains a timer that is updating every 15 seconds. 该应用程序包含一个计时器,该计时器每15秒更新一次。
When the app is navigating to background, I am doing the following 当应用导航到后台时,我正在执行以下操作
- (void)applicationDidEnterBackground:(UIApplication *)application {
UIApplication* app = [UIApplication sharedApplication];
self.bgTaskID = [app beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"background task %lu expired", (unsigned long)self.bgTaskID);
[app endBackgroundTask:self.bgTaskID];
self.bgTaskID = UIBackgroundTaskInvalid;
}];
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(initializeLocationManager) userInfo:nil repeats:NO];
if(self.timerLocationBackground)
{
[self.timerLocationBackground invalidate];
self.timerLocationBackground = nil;
}
self.timerLocationBackground = [NSTimer scheduledTimerWithTimeInterval:15
target:self
selector:@selector(initializeLocationManager)
userInfo:nil
repeats:YES];}`
The initializeLocationManager is below initializeLocationManager在下面
-(void)initializeLocationManager
{
if(!self.locationManager)
self.locationManager = [[CLLocationManager alloc] init];
else
[self.locationManager stopUpdatingLocation];
if ((![CLLocationManager locationServicesEnabled])
|| ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted)
|| ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied))
{
//user has disabled his location
}
else
{
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
[self.locationManager setAllowsBackgroundLocationUpdates:YES];
[self.locationManager startUpdatingLocation];
}
}
When I navigate back to the application after 10 minutes for ex, my timer is being stopped at 3 min which is the time for the app to be suspended. 例如,当我在10分钟后导航回到该应用程序时,我的计时器将在3分钟停止,这是该应用程序被暂停的时间。
My code when the app get back to foreground is the below: 当应用回到前台时,我的代码如下:
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
//
//Remove the baground task
//
if (self.bgTaskID != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:self.bgTaskID];
self.bgTaskID = UIBackgroundTaskInvalid;
}
[self.locationManager stopUpdatingLocation];
Any help? 有什么帮助吗?
Should be something like this. 应该是这样的。 I've switched over to swift myself, so my objective-c might not be perfect. 我已经改了动身,所以我的Objective-C可能并不完美。
- (void)applicationDidEnterBackground:(UIApplication *)application {
[self keepAwakeInBackground ] ;
/// rest of your function to set up timers
}
- (void) keepAwakeInBackground {
//Remove the old background task, if there was one
if (self.bgTaskID != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:self.bgTaskID];
self.bgTaskID = UIBackgroundTaskInvalid;
}
/*
* You probably want something here to decide that the app should really be suspended here
* if ( ) return
*/
// Set up new background task
UIApplication* app = [UIApplication sharedApplication];
self.bgTaskID = [app beginBackgroundTaskWithExpirationHandler:^{
[self keepAwakeInBackground]
}];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.