From fb2df21037795589d71489c64a5e17cbcb5f1254 Mon Sep 17 00:00:00 2001 From: Jesse Bounds Date: Fri, 21 Jul 2017 10:29:49 -0700 Subject: [ios] Add option to disable background location updates --- platform/ios/src/MGLLocationManager.h | 1 + platform/ios/src/MGLLocationManager.m | 10 ++++++++-- platform/ios/src/MGLMapboxEvents.m | 4 ++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/platform/ios/src/MGLLocationManager.h b/platform/ios/src/MGLLocationManager.h index ea23801813..65d2ee9b84 100644 --- a/platform/ios/src/MGLLocationManager.h +++ b/platform/ios/src/MGLLocationManager.h @@ -6,6 +6,7 @@ @interface MGLLocationManager : NSObject @property (nonatomic, weak) id delegate; +@property (nonatomic) BOOL allowsBackgroundLocationUpdates; - (void)startUpdatingLocation; - (void)stopUpdatingLocation; diff --git a/platform/ios/src/MGLLocationManager.m b/platform/ios/src/MGLLocationManager.m index b0d2e17d5d..f1a109876d 100644 --- a/platform/ios/src/MGLLocationManager.m +++ b/platform/ios/src/MGLLocationManager.m @@ -23,7 +23,8 @@ static NSString * const MGLLocationManagerRegionIdentifier = @"MGLLocationManage self = [super init]; if (self) { NSArray *backgroundModes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIBackgroundModes"]; - _hostAppHasBackgroundCapability = [backgroundModes containsObject:@"location"]; + _hostAppHasBackgroundCapability = [backgroundModes containsObject:@"location"]; + _allowsBackgroundLocationUpdates = YES; } return self; } @@ -55,6 +56,11 @@ static NSString * const MGLLocationManagerRegionIdentifier = @"MGLLocationManage } } +- (void)setAllowsBackgroundLocationUpdates:(BOOL)allowsBackgroundLocationUpdates { + _allowsBackgroundLocationUpdates = allowsBackgroundLocationUpdates; + self.standardLocationManager.allowsBackgroundLocationUpdates = _allowsBackgroundLocationUpdates; +} + #pragma mark - Utilities - (void)configurePassiveStandardLocationManager { @@ -82,7 +88,7 @@ static NSString * const MGLLocationManagerRegionIdentifier = @"MGLLocationManage [self startBackgroundTimeoutTimer]; // On iOS 9 and above also allow background location updates if ([self.standardLocationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]) { - self.standardLocationManager.allowsBackgroundLocationUpdates = YES; + self.standardLocationManager.allowsBackgroundLocationUpdates = _allowsBackgroundLocationUpdates; } } diff --git a/platform/ios/src/MGLMapboxEvents.m b/platform/ios/src/MGLMapboxEvents.m index 4f1413d300..e31fdf3515 100644 --- a/platform/ios/src/MGLMapboxEvents.m +++ b/platform/ios/src/MGLMapboxEvents.m @@ -140,6 +140,7 @@ const NSTimeInterval MGLFlushInterval = 180; @"MGLMapboxAccountType": accountTypeNumber ?: @0, @"MGLMapboxMetricsEnabled": @YES, @"MGLMapboxMetricsDebugLoggingEnabled": @NO, + @"MGLMapboxMetricsAllowsBackgroundLocationUpdates": @YES, }]; } } @@ -272,6 +273,9 @@ const NSTimeInterval MGLFlushInterval = 180; [self pauseMetricsCollection]; return; } + + BOOL allowsBackgroundLocationUpdates = [[NSUserDefaults standardUserDefaults] boolForKey:@"MGLMapboxMetricsAllowsBackgroundLocationUpdates"]; + self.locationManager.allowsBackgroundLocationUpdates = allowsBackgroundLocationUpdates; // Toggle pause based on current pause state, user opt-out state, and low-power state. BOOL enabled = [[self class] isEnabled]; -- cgit v1.2.1