From a55f9a3fc21bd21c4e610e909d9039748b8b05e1 Mon Sep 17 00:00:00 2001 From: vijay Date: Fri, 10 Jul 2020 16:35:43 -0700 Subject: [PATCH] feat(ios): expose new APIs to use location AccuracyAuthorization Fixes TIMOB-27976 --- iphone/Classes/GeolocationModule.h | 15 +++++++++++ iphone/Classes/GeolocationModule.m | 42 +++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/iphone/Classes/GeolocationModule.h b/iphone/Classes/GeolocationModule.h index 49ce55c2f0b..a45120d5862 100644 --- a/iphone/Classes/GeolocationModule.h +++ b/iphone/Classes/GeolocationModule.h @@ -14,6 +14,7 @@ NSString *const kTiGeolocationUsageDescriptionWhenInUse = @"NSLocationWhenInUseUsageDescription"; NSString *const kTiGeolocationUsageDescriptionAlways = @"NSLocationAlwaysUsageDescription"; NSString *const kTiGeolocationUsageDescriptionAlwaysAndWhenInUse = @"NSLocationAlwaysAndWhenInUseUsageDescription"; +NSString *const kTiGeolocationTemporaryUsageDescriptionDictionary = @"NSLocationTemporaryUsageDescriptionDictionary"; @protocol GeolocationExports @@ -21,6 +22,7 @@ NSString *const kTiGeolocationUsageDescriptionAlwaysAndWhenInUse = @"NSLocationA 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); @@ -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); @@ -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); @@ -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 { diff --git a/iphone/Classes/GeolocationModule.m b/iphone/Classes/GeolocationModule.m index 016edbde7bd..e72aa0a0730 100644 --- a/iphone/Classes/GeolocationModule.m +++ b/iphone/Classes/GeolocationModule.m @@ -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); @@ -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);