@@ -11,21 +11,25 @@ @implementation MockLocationDetector
1111
1212RCT_EXPORT_METHOD(isMockingLocation:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
1313 dispatch_async (dispatch_get_main_queue (), ^{
14+ if (![self getCachedVersionOrFail: resolve reject: reject]) {
15+ return ;
16+ }
17+
1418 if ([self checkIfGPSIsEnabled ] == NO ) {
1519 reject (@" 0" , @" GPS is not enabled" , nil );
16-
20+
1721 return ;
1822 }
19-
23+
2024 if ([self hasLocationPermission ] == NO ) {
2125 reject (@" 1" , @" You have no permission to access location" , nil );
22-
26+
2327 return ;
2428 }
2529
2630 self.resolve = resolve;
2731 self.reject = reject;
28-
32+
2933 self.locationManager = [[CLLocationManager alloc ] init ];
3034 self.locationManager .delegate = self;
3135
@@ -46,6 +50,7 @@ - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray
4650 @" isLocationMocked" : @(sourceInformation.isSimulatedBySoftware )
4751 };
4852
53+ self.cachedIsLocationMocked = [NSNumber numberWithBool: sourceInformation.isSimulatedBySoftware];
4954 self.resolve (dict);
5055 } else {
5156 self.reject (@" 2" , @" Couldn't determine if location is mocked" , nil );
@@ -64,6 +69,32 @@ - (void)cleanUp {
6469 self.locationManager = nil ;
6570 self.resolve = nil ;
6671 self.reject = nil ;
72+ self.cachedIsLocationMocked = nil ;
73+ }
74+
75+ - (void )dealloc {
76+ [self cleanUp ];
77+ }
78+
79+ - (BOOL )getCachedVersionOrFail : (RCTPromiseResolveBlock)resolve reject : (RCTPromiseRejectBlock)reject {
80+ if (!self.resolve && !self.reject ) {
81+ return YES ;
82+ }
83+
84+ // other promise in progress
85+ if (self.cachedIsLocationMocked != nil ) {
86+ NSDictionary *dict = @{
87+ @" isLocationMocked" : @([self .cachedIsLocationMocked boolValue ])
88+ };
89+
90+ resolve (dict);
91+
92+ return NO ;
93+ }
94+
95+ reject (@" 2" , @" Couldn't determine if location is mocked" , nil );
96+
97+ return NO ;
6798}
6899
69100- (BOOL )checkIfGPSIsEnabled {
@@ -72,7 +103,7 @@ - (BOOL)checkIfGPSIsEnabled {
72103
73104- (BOOL )hasLocationPermission {
74105 CLAuthorizationStatus status = [CLLocationManager authorizationStatus ];
75-
106+
76107 return status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse ;
77108}
78109
0 commit comments