From 15b9f3171a1c41d5a4c2b454fcce9b92350e28f2 Mon Sep 17 00:00:00 2001 From: Jason Wray Date: Wed, 20 Jan 2016 20:04:12 -0500 Subject: [PATCH] [ios] Refactor MGLMapView's location services MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Pause location services when the app goes to sleep; address iOS 8 blue bar issue. - Only ask for location permissions if not already granted/denied. - Cleanup usage of `.isDormant` → `.dormant` Partially addresses #2945, for which a full and complete fix waits until #4030. --- platform/ios/src/MGLMapView.mm | 85 ++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 35 deletions(-) diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index c4f6cd39a..83904f168 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -760,7 +760,7 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) // This is the delegate of the GLKView object's display call. - (void)glkView:(__unused GLKView *)view drawInRect:(__unused CGRect)rect { - if ( ! self.isDormant) + if ( ! self.dormant) { CGFloat zoomFactor = _mbglMap->getMaxZoom() - _mbglMap->getMinZoom() + 1; CGFloat cpuFactor = (CGFloat)[[NSProcessInfo processInfo] processorCount]; @@ -906,7 +906,7 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) { MGLAssertIsMainThread(); - if ( ! self.isDormant) + if ( ! self.dormant) { [self validateDisplayLink]; self.dormant = YES; @@ -957,6 +957,8 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) { self.dormant = YES; + [self validateLocationServices]; + [MGLMapboxEvents flush]; _displayLink.paused = YES; @@ -1006,6 +1008,8 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) _mbglMap->resume(); _displayLink.paused = NO; + + [self validateLocationServices]; } } @@ -3022,6 +3026,48 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) #pragma mark - User Location - +- (void)validateLocationServices +{ + BOOL shouldEnableLocationServices = self.showsUserLocation && !self.dormant; + + if (shouldEnableLocationServices && ! self.locationManager) + { + self.locationManager = [[CLLocationManager alloc] init]; + +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 + if ([CLLocationManager instancesRespondToSelector:@selector(requestWhenInUseAuthorization)] && [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) + { + BOOL hasLocationDescription = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"] || [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]; + if (!hasLocationDescription) + { + [NSException raise:@"Missing Location Services usage description" format: + @"In iOS 8 and above, this app must have a value for NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription in its Info.plist."]; + } + + if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]) + { + [self.locationManager requestAlwaysAuthorization]; + } + else if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]) + { + [self.locationManager requestWhenInUseAuthorization]; + } + } +#endif + + self.locationManager.headingFilter = 5.0; + self.locationManager.delegate = self; + [self.locationManager startUpdatingLocation]; + } + else if ( ! shouldEnableLocationServices && self.locationManager) + { + [self.locationManager stopUpdatingLocation]; + [self.locationManager stopUpdatingHeading]; + self.locationManager.delegate = nil; + self.locationManager = nil; + } +} + - (void)setShowsUserLocation:(BOOL)showsUserLocation { if (showsUserLocation == _showsUserLocation || _isTargetingInterfaceBuilder) return; @@ -3039,42 +3085,11 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) self.userLocationAnnotationView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin); - self.locationManager = [CLLocationManager new]; - -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 - // enable iOS 8+ location authorization API - // - if ([CLLocationManager instancesRespondToSelector:@selector(requestWhenInUseAuthorization)]) - { - BOOL hasLocationDescription = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"] || - [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]; - if (!hasLocationDescription) - { - [NSException raise:@"Missing Location Services usage description" format: - @"In iOS 8 and above, this app must have a value for NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription in its Info.plist."]; - } - // request location permissions, if both keys exist ask for less permissive - if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]) - { - [self.locationManager requestWhenInUseAuthorization]; - } - else if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]) - { - [self.locationManager requestAlwaysAuthorization]; - } - } -#endif - - self.locationManager.headingFilter = 5.0; - self.locationManager.delegate = self; - [self.locationManager startUpdatingLocation]; + [self validateLocationServices]; } else { - [self.locationManager stopUpdatingLocation]; - [self.locationManager stopUpdatingHeading]; - self.locationManager.delegate = nil; - self.locationManager = nil; + [self validateLocationServices]; if ([self.delegate respondsToSelector:@selector(mapViewDidStopLocatingUser:)]) {