Skip to content

Commit

Permalink
feat(ios): expose new APIs to use location AccuracyAuthorization
Browse files Browse the repository at this point in the history
Fixes TIMOB-27976
  • Loading branch information
vijaysingh-axway authored and sgtcoolguy committed Sep 8, 2020
1 parent 00f4b58 commit a55f9a3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
15 changes: 15 additions & 0 deletions iphone/Classes/GeolocationModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
NSString *const kTiGeolocationUsageDescriptionWhenInUse = @"NSLocationWhenInUseUsageDescription";
NSString *const kTiGeolocationUsageDescriptionAlways = @"NSLocationAlwaysUsageDescription";
NSString *const kTiGeolocationUsageDescriptionAlwaysAndWhenInUse = @"NSLocationAlwaysAndWhenInUseUsageDescription";
NSString *const kTiGeolocationTemporaryUsageDescriptionDictionary = @"NSLocationTemporaryUsageDescriptionDictionary";

@protocol GeolocationExports <JSExport>

// accuracy constants
CONSTANT(NSNumber *, ACCURACY_BEST_FOR_NAVIGATION);
CONSTANT(NSNumber *, ACCURACY_HIGH);
CONSTANT(NSNumber *, ACCURACY_LOW);
CONSTANT(NSNumber *, ACCURACY_REDUCED);

// iOS-specific values, (deprecated on Android)
CONSTANT(NSNumber *, ACCURACY_BEST);
Expand All @@ -42,6 +44,10 @@ CONSTANT(NSNumber *, AUTHORIZATION_RESTRICTED);
CONSTANT(NSNumber *, AUTHORIZATION_UNKNOWN);
CONSTANT(NSNumber *, AUTHORIZATION_WHEN_IN_USE);

//Accuracy Authorization to use location
CONSTANT(NSNumber *, ACCURACY_AUTHORIZATION_FULL);
CONSTANT(NSNumber *, ACCURACY_AUTHORIZATION_REDUCED);

// Error codes
CONSTANT(NSNumber *, ERROR_DENIED);
CONSTANT(NSNumber *, ERROR_HEADING_FAILURE);
Expand All @@ -61,6 +67,9 @@ READONLY_PROPERTY(bool, hasCompass, HasCompass);
PROPERTY(CLLocationDegrees, headingFilter, HeadingFilter);
READONLY_PROPERTY(NSString *, lastGeolocation, LastGeolocation);
READONLY_PROPERTY(CLAuthorizationStatus, locationServicesAuthorization, LocationServicesAuthorization);
#if IS_SDK_IOS_14
READONLY_PROPERTY(CLAccuracyAuthorization, locationAccuracyAuthorization, AccuracyAuthorization);
#endif
READONLY_PROPERTY(bool, locationServicesEnabled, LocationServicesEnabled);
PROPERTY(bool, pauseLocationUpdateAutomatically, PauseLocationUpdateAutomatically);
PROPERTY(bool, showBackgroundLocationIndicator, ShowBackgroundLocationIndicator);
Expand All @@ -85,6 +94,12 @@ JSExportAs(reverseGeocoder,
: (double)longitude withCallback
: (JSValue *)callback);

#if IS_SDK_IOS_14
JSExportAs(requestTemporaryFullAccuracyAuthorization,
-(void)requestTemporaryFullAccuracyAuthorization
: (NSString *)purposeString withCallback
: (JSValue *)callback);
#endif
@end

@interface GeolocationModule : ObjcProxy <GeolocationExports, CLLocationManagerDelegate> {
Expand Down
42 changes: 41 additions & 1 deletion iphone/Classes/GeolocationModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,13 @@ - (void)restart:(id)arg
MAKE_SYSTEM_PROP_DBL(ACCURACY_KILOMETER, kCLLocationAccuracyKilometer);
MAKE_SYSTEM_PROP_DBL(ACCURACY_THREE_KILOMETERS, kCLLocationAccuracyThreeKilometers);
MAKE_SYSTEM_PROP_DBL(ACCURACY_LOW, kCLLocationAccuracyThreeKilometers);
MAKE_SYSTEM_PROP(ACCURACY_BEST_FOR_NAVIGATION, kCLLocationAccuracyBestForNavigation); //Since 2.1.3
MAKE_SYSTEM_PROP_DBL(ACCURACY_BEST_FOR_NAVIGATION, kCLLocationAccuracyBestForNavigation);
#if IS_SDK_IOS_14
MAKE_SYSTEM_PROP_DBL(ACCURACY_REDUCED, kCLLocationAccuracyReduced);

MAKE_SYSTEM_PROP(ACCURACY_AUTHORIZATION_FULL, CLAccuracyAuthorizationFullAccuracy);
MAKE_SYSTEM_PROP(ACCURACY_AUTHORIZATION_REDUCED, CLAccuracyAuthorizationReducedAccuracy);
#endif

MAKE_SYSTEM_PROP(AUTHORIZATION_UNKNOWN, kCLAuthorizationStatusNotDetermined);
MAKE_SYSTEM_PROP(AUTHORIZATION_AUTHORIZED, kCLAuthorizationStatusAuthorizedAlways);
Expand Down Expand Up @@ -847,6 +853,40 @@ - (void)requestLocationPermissions:(CLAuthorizationStatus)authorizationType with
}
}

#if IS_SDK_IOS_14
- (void)requestTemporaryFullAccuracyAuthorization:(NSString *)purposeKey withCallback:(JSValue *)callback
{
if (![TiUtils isIOSVersionOrGreater:@"14.0"]) {
NSMutableDictionary *propertiesDict = [TiUtils dictionaryWithCode:1 message:@"Supported on iOS 14+"];
[callback callWithArguments:@[ propertiesDict ]];
return;
}
NSDictionary *descriptionDict = [[NSBundle mainBundle] objectForInfoDictionaryKey:kTiGeolocationTemporaryUsageDescriptionDictionary];
if (!descriptionDict || ![descriptionDict valueForKey:purposeKey]) {
DebugLog(@"[WARN] Add %@ key with purpose key %@ in info.plist", kTiGeolocationTemporaryUsageDescriptionDictionary, purposeKey);
}
[[self locationPermissionManager] requestTemporaryFullAccuracyAuthorizationWithPurposeKey:purposeKey
completion:^(NSError *_Nullable error) {
NSMutableDictionary *propertiesDict = [TiUtils dictionaryWithCode:0 message:nil];
if (error != nil) {
propertiesDict = [TiUtils dictionaryWithCode:1 message:error.description];
} else {
propertiesDict[@"accuracyAuthorization"] = @([[self locationPermissionManager] accuracyAuthorization]);
}
[callback callWithArguments:@[ propertiesDict ]];
}];
}

- (CLAccuracyAuthorization)locationAccuracyAuthorization
{
if (![TiUtils isIOSVersionOrGreater:@"14.0"]) {
DebugLog(@"[ERROR] This property is available on iOS 14 and above.");
return -1;
}
return [[self locationPermissionManager] accuracyAuthorization];
}
#endif

READWRITE_IMPL(bool, allowsBackgroundLocationUpdates, AllowsBackgroundLocationUpdates);
GETTER_IMPL(NSString *, lastGeolocation, LastGeolocation);
READWRITE_IMPL(bool, showCalibration, ShowCalibration);
Expand Down

0 comments on commit a55f9a3

Please sign in to comment.