Skip to content

Commit

Permalink
fix(ios): minor fixes for Ti.Geolocation#requestLocationPermissions()
Browse files Browse the repository at this point in the history
* tweak requestLocationPermissions promise rejection to be Error object
* guard for when no callback supplied
* fix management of callback/promise
  • Loading branch information
sgtcoolguy committed Feb 22, 2021
1 parent 2155d0a commit 82ff625
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion iphone/Classes/GeolocationModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ JSExportAs(requestTemporaryFullAccuracyAuthorization,
BOOL trackSignificantLocationChange;
bool allowsBackgroundLocationUpdates;
BOOL showBackgroundLocationIndicator;
JSManagedValue *authorizationCallback;
JSValue *authorizationCallback;
KrollPromise *authorizationPromise;
CLAuthorizationStatus requestedAuthorizationStatus;

Expand Down
22 changes: 10 additions & 12 deletions iphone/Classes/GeolocationModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ - (void)dealloc
RELEASE_TO_NIL(purpose);
RELEASE_TO_NIL(lock);
RELEASE_TO_NIL(lastLocationDict);
RELEASE_TO_NIL(authorizationCallback);
RELEASE_TO_NIL(authorizationPromise);
[super dealloc];
}

Expand Down Expand Up @@ -834,15 +836,13 @@ - (bool)hasLocationPermissions:(CLAuthorizationStatus)authorizationType
- (JSValue *)requestLocationPermissions:(CLAuthorizationStatus)authorizationType withCallback:(JSValue *)callback
{
// Store the authorization callback for later usage
if (callback != nil) {
// FIXME: What about multiple calls before resolution. We should store in an array like we do for getCurrentPosition
if (callback != nil && ![callback isUndefined]) {
if (authorizationCallback != nil) {
JSValue *actualCallback = [authorizationCallback value];
[actualCallback.context.virtualMachine removeManagedReference:authorizationCallback withOwner:self];
[authorizationCallback release];
authorizationCallback = nil;
}
authorizationCallback = [[JSManagedValue managedValueWithValue:callback andOwner:self] retain];
[callback.context.virtualMachine addManagedReference:authorizationCallback withOwner:self];
authorizationCallback = [callback retain];
}

// Promise to return
Expand All @@ -862,8 +862,7 @@ - (JSValue *)requestLocationPermissions:(CLAuthorizationStatus)authorizationType
[self executeAndReleaseCallbackWithCode:0 andMessage:nil];
return promise.JSValue;
} else if (currentPermissionLevel == kCLAuthorizationStatusDenied) {
NSString *message = @"The user denied access to use location services.";
[self executeAndReleaseCallbackWithCode:1 andMessage:message];
[self executeAndReleaseCallbackWithCode:1 andMessage:@"The user denied access to use location services."];
return promise.JSValue;
}

Expand Down Expand Up @@ -977,18 +976,17 @@ - (void)executeAndReleaseCallbackWithCode:(NSInteger)code andMessage:(NSString *
if (code == 0) {
[authorizationPromise resolve:invocationArray];
} else {
// FIXME: We should be rejecting with an Error
[authorizationPromise reject:invocationArray];
[authorizationPromise rejectWithErrorMessage:message];
}
[authorizationPromise release];
authorizationPromise = nil;
}

if (authorizationCallback != nil) {
JSValue *actualCallback = [authorizationCallback value];
[actualCallback callWithArguments:invocationArray];
if (![authorizationCallback isUndefined]) {
[authorizationCallback callWithArguments:invocationArray];
}
// release the stored callback
[actualCallback.context.virtualMachine removeManagedReference:authorizationCallback withOwner:self];
[authorizationCallback release];
authorizationCallback = nil;
}
Expand Down

0 comments on commit 82ff625

Please sign in to comment.