Skip to content

Commit

Permalink
Merge pull request #11 from mapbox/move-background-guard
Browse files Browse the repository at this point in the history
Move background modes guard to location manager wrapper
  • Loading branch information
boundsj authored Sep 28, 2017
2 parents 533b32c + 7eb5cd2 commit f0ccdb7
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 66 deletions.
1 change: 1 addition & 0 deletions MapboxMobileEvents/MMECLLocationManagerWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@property (nonatomic) CLLocationAccuracy desiredAccuracy;
@property (nonatomic) CLLocationDistance distanceFilter;
@property (nonatomic, copy, readonly) NSSet<__kindof CLRegion *> *monitoredRegions;
@property (nonatomic) BOOL hostAppHasBackgroundCapability;

- (CLAuthorizationStatus)authorizationStatus;
- (void)startUpdatingLocation;
Expand Down
9 changes: 7 additions & 2 deletions MapboxMobileEvents/MMECLLocationManagerWrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ @interface MMECLLocationManagerWrapper ()

@implementation MMECLLocationManagerWrapper

@synthesize hostAppHasBackgroundCapability;

- (instancetype)init {
self = [super init];
if (self) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
NSArray *backgroundModes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIBackgroundModes"];
self.hostAppHasBackgroundCapability = [backgroundModes containsObject:@"location"];
}
return self;
}
Expand Down Expand Up @@ -46,14 +50,15 @@ - (void)stopMonitoringForRegion:(CLRegion *)region {
}

- (void)setAllowsBackgroundLocationUpdates:(BOOL)allowsBackgroundLocationUpdates {
if ([self.locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]) {
if ([self.locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]
&& self.hostAppHasBackgroundCapability) {
self.locationManager.allowsBackgroundLocationUpdates = allowsBackgroundLocationUpdates;
}
}

- (BOOL)allowsBackgroundLocationUpdates {
if ([self.locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]) {
return self.locationManager.allowsBackgroundLocationUpdates;
return !self.hostAppHasBackgroundCapability ? NO : self.locationManager.allowsBackgroundLocationUpdates;
}
return NO;
}
Expand Down
7 changes: 2 additions & 5 deletions MapboxMobileEvents/MMELocationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ @interface MMELocationManager ()

@property (nonatomic) id<MMEUIApplicationWrapper> application;
@property (nonatomic) id<MMECLLocationManagerWrapper> locationManager;
@property (nonatomic) BOOL hostAppHasBackgroundCapability;
@property (nonatomic, getter=isUpdatingLocation, readwrite) BOOL updatingLocation;
@property (nonatomic) NSDate *backgroundLocationServiceTimeoutAllowedDate;
@property (nonatomic) NSTimer *backgroundLocationServiceTimeoutTimer;
Expand All @@ -26,9 +25,7 @@ @implementation MMELocationManager
- (instancetype)init {
self = [super init];
if (self) {
NSArray *backgroundModes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIBackgroundModes"];
_hostAppHasBackgroundCapability = [backgroundModes containsObject:@"location"];
_application = [[MMEUIApplicationWrapper alloc] init];
_application = [[MMEUIApplicationWrapper alloc] init];
}
return self;
}
Expand Down Expand Up @@ -89,7 +86,7 @@ - (void)startLocationServices {

// If the host app can run in the background with `always` location permissions then allow background
// updates and start the significant location change service and background timeout timer
if (self.hostAppHasBackgroundCapability && authorizedAlways) {
if (authorizedAlways && self.locationManager.hostAppHasBackgroundCapability) {
[self.locationManager startMonitoringSignificantLocationChanges];
[self startBackgroundTimeoutTimer];
self.locationManager.allowsBackgroundLocationUpdates = YES;
Expand Down
1 change: 1 addition & 0 deletions MapboxMobileEventsTests/MMECLLocationManagerWrapperFake.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ @implementation MMECLLocationManagerWrapperFake
@synthesize desiredAccuracy;
@synthesize distanceFilter;
@synthesize monitoredRegions;
@synthesize hostAppHasBackgroundCapability;

- (CLAuthorizationStatus)authorizationStatus {
return self.stub_authorizationStatus;
Expand Down
Loading

0 comments on commit f0ccdb7

Please sign in to comment.