Skip to content

Commit

Permalink
fix(ios): error code/object for Ti.Geolocation permissions/position
Browse files Browse the repository at this point in the history
* fix merge of dictionary for authorizationStatus change so object has code/error props
* properly report error getting location when underlying error code is 0
(use code of -1, so success is false)
* clean up Geolocation code, remove unnecessary tempManager
  • Loading branch information
sgtcoolguy committed Feb 22, 2021
1 parent 5db8321 commit 974a7f6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 58 deletions.
1 change: 0 additions & 1 deletion iphone/Classes/GeolocationModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ JSExportAs(requestTemporaryFullAccuracyAuthorization,

@interface GeolocationModule : ObjcModule <GeolocationExports, CLLocationManagerDelegate> {
CLLocationManager *locationManager;
CLLocationManager *tempManager; // Our 'fakey' manager for handling certain <=3.2 requests
CLLocationManager *locationPermissionManager; // used for just permissions requests

CLLocationAccuracy accuracy;
Expand Down
104 changes: 47 additions & 57 deletions iphone/Classes/GeolocationModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ - (void)dealloc
{
[self shutdownLocationManager];
[self releaseSingleshotListeners];
RELEASE_TO_NIL(tempManager);
RELEASE_TO_NIL(locationPermissionManager);
RELEASE_TO_NIL(purpose);
RELEASE_TO_NIL(lock);
Expand Down Expand Up @@ -324,7 +323,6 @@ - (CLLocationManager *)locationManager
{
[lock lock];
if (locationManager == nil) {
RELEASE_TO_NIL(tempManager);
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
if (!trackSignificantLocationChange) {
Expand Down Expand Up @@ -385,73 +383,60 @@ - (CLLocationManager *)locationManager
return locationManager;
}

// this is useful for a few methods below that need to use an instance but we
// don't necessarily want to hold on to this guy
- (CLLocationManager *)tempLocationManager
{
if (locationManager != nil) {
// if we have an instance, just use it
return locationManager;
}

if (tempManager == nil) {
tempManager = [[CLLocationManager alloc] init];
}
return tempManager;
}

- (void)startStopLocationManagerIfNeeded
{
BOOL startHeading = NO;
BOOL startLocation = NO;

if (singleHeading != nil && [singleHeading count] > 0) {
// if we have queued getCurrentHeading() calls or a 'heading' event listener, startHeading
if ((singleHeading != nil && [singleHeading count] > 0) || [self _hasListeners:@"heading"]) {
startHeading = YES;
}
if (singleLocation != nil && [singleLocation count] > 0) {
// if we have queued getCurrentPosition() calls or a 'location' event listener, startLocation
if ((singleLocation != nil && [singleLocation count] > 0) || [self _hasListeners:@"location"]) {
startLocation = YES;
}
if (!startHeading && [self _hasListeners:@"heading"]) {
startHeading = YES;

// Basically we need to guard against "starting" location manager unnecessarily
// So if both are NO *and* it's already nil, return early
if (!startHeading && !startLocation && locationManager == nil) {
return;
}
if (!startLocation && [self _hasListeners:@"location"]) {
startLocation = YES;

// Otherwise toggle based on if we're already tracking heading/location
CLLocationManager *lm = [self locationManager];

// heading
if (startHeading && !trackingHeading) { // track heading if we aren't already...
[lm startUpdatingHeading];
trackingHeading = YES;
} else if (!startHeading && trackingHeading) { // turn off heading if we were and don't want to anymore...
trackingHeading = NO;
[lm stopUpdatingHeading];
}

if (startHeading || startLocation) {
CLLocationManager *lm = [self locationManager];
if (startHeading && !trackingHeading) {
[lm startUpdatingHeading];
trackingHeading = YES;
}
if (startLocation && !trackingLocation) {
if (trackSignificantLocationChange) {
[lm startMonitoringSignificantLocationChanges];
} else {
[lm startUpdatingLocation];
}
trackingLocation = YES;
}
} else if ((!startHeading || !startLocation) && locationManager != nil) {
CLLocationManager *lm = [self locationManager];
if (!startHeading && trackingHeading) {
trackingHeading = NO;
[lm stopUpdatingHeading];
}
if (!startLocation && trackingLocation) {
trackingLocation = NO;
if (trackSignificantLocationChange) {
[lm stopMonitoringSignificantLocationChanges];
} else {
[lm stopUpdatingLocation];
}
// location
if (startLocation && !trackingLocation) { // track location if we aren't already...
if (trackSignificantLocationChange) {
[lm startMonitoringSignificantLocationChanges];
} else {
[lm startUpdatingLocation];
}
if ((!startHeading && !startLocation) || (!trackingHeading && !trackingLocation)) {
[self shutdownLocationManager];
trackingLocation = NO;
trackingHeading = NO;
trackingLocation = YES;
} else if (!startLocation && trackingLocation) { // turn off location if we were and don't want to anymore...
trackingLocation = NO;
if (trackSignificantLocationChange) {
[lm stopMonitoringSignificantLocationChanges];
} else {
[lm stopUpdatingLocation];
}
}

// If we turned both heading and location off...
// TODO: Move this to top? it turns off heading/location under the covers if necessary
if (!trackingHeading && !trackingLocation) {
[self shutdownLocationManager]; // shut down the location manager
}
}

- (void)_listenerAdded:(NSString *)type count:(int)count
Expand Down Expand Up @@ -906,7 +891,7 @@ - (JSValue *)requestLocationPermissions:(CLAuthorizationStatus)authorizationType

if (errorMessage != nil) {
NSLog(@"[ERROR] %@", errorMessage);
[self executeAndReleaseCallbackWithCode:(errorMessage == nil) ? 0 : 1 andMessage:errorMessage];
[self executeAndReleaseCallbackWithCode:1 andMessage:errorMessage];
RELEASE_TO_NIL(errorMessage);
}
return promise.JSValue;
Expand Down Expand Up @@ -972,7 +957,7 @@ - (void)executeAndReleaseCallbackWithCode:(NSInteger)code andMessage:(NSString *
{
NSMutableDictionary *fullDict = [TiUtils dictionaryWithCode:code message:message];
if (dict != nil) {
[fullDict setDictionary:dict];
[fullDict addEntriesFromDictionary:dict];
}
NSArray *invocationArray = @[ fullDict ];

Expand Down Expand Up @@ -1198,7 +1183,12 @@ - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLoca

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSMutableDictionary *event = [TiUtils dictionaryWithCode:[error code] message:[TiUtils messageFromError:error]];
// Error code may be 0 here, but then we report success!
NSInteger code = error.code;
if (code == 0) {
code = -1; // explicitly use non-zero code to force success to be false!
}
NSMutableDictionary *event = [TiUtils dictionaryWithCode:code message:[TiUtils messageFromError:error]];

if ([self _hasListeners:@"location"]) {
[self fireEvent:@"location" withDict:event];
Expand Down

0 comments on commit 974a7f6

Please sign in to comment.