diff --git a/geolocator_apple/darwin/geolocator_apple/Sources/geolocator_apple/Handlers/LocationServiceStreamHandler.m b/geolocator_apple/darwin/geolocator_apple/Sources/geolocator_apple/Handlers/LocationServiceStreamHandler.m index e1da68293..01492a792 100644 --- a/geolocator_apple/darwin/geolocator_apple/Sources/geolocator_apple/Handlers/LocationServiceStreamHandler.m +++ b/geolocator_apple/darwin/geolocator_apple/Sources/geolocator_apple/Handlers/LocationServiceStreamHandler.m @@ -33,7 +33,7 @@ - (FlutterError * _Nullable)onListenWithArguments:(id _Nullable)arguments eventS return nil; } -- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{ +- (void)_notifyServiceStatus { dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ BOOL isEnabled = [CLLocationManager locationServicesEnabled]; dispatch_async(dispatch_get_main_queue(), ^(void) { @@ -46,4 +46,17 @@ - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatu }); } +// iOS 14+ / macOS 11+: preferred delegate callback +- (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager { + [self _notifyServiceStatus]; +} + +// Fallback for iOS < 14 / macOS < 11 +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-implementations" +- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{ + [self _notifyServiceStatus]; +} +#pragma clang diagnostic pop + @end diff --git a/geolocator_apple/darwin/geolocator_apple/Sources/geolocator_apple/Handlers/PermissionHandler.m b/geolocator_apple/darwin/geolocator_apple/Sources/geolocator_apple/Handlers/PermissionHandler.m index 265401aad..cbcfea207 100644 --- a/geolocator_apple/darwin/geolocator_apple/Sources/geolocator_apple/Handlers/PermissionHandler.m +++ b/geolocator_apple/darwin/geolocator_apple/Sources/geolocator_apple/Handlers/PermissionHandler.m @@ -37,14 +37,22 @@ - (CLAuthorizationStatus) checkPermission { if (@available(iOS 14, macOS 11, *)) { return [self.getLocationManager authorizationStatus]; } else { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" return [CLLocationManager authorizationStatus]; +#pragma clang diagnostic pop } } - (void) requestPermission:(PermissionConfirmation)confirmationHandler errorHandler:(PermissionError)errorHandler { - // When we already have permission we don't have to request it again + // When we already have permission we don't have to request it again. + // Use the class method directly (with warning suppression) to avoid + // lazily creating and retaining a CLLocationManager on the early-return path. +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" CLAuthorizationStatus authorizationStatus = CLLocationManager.authorizationStatus; +#pragma clang diagnostic pop if (authorizationStatus != kCLAuthorizationStatusNotDetermined) { confirmationHandler(authorizationStatus); return; @@ -104,6 +112,23 @@ - (BOOL) containsLocationAlwaysDescription { } #endif +// iOS 14+ / macOS 11+: preferred delegate callback +- (void) locationManagerDidChangeAuthorization:(CLLocationManager *)manager { + CLAuthorizationStatus status = [self checkPermission]; + if (status == kCLAuthorizationStatusNotDetermined) { + return; + } + + if (self.confirmationHandler) { + self.confirmationHandler(status); + } + + [self cleanUp]; +} + +// Fallback for iOS < 14 / macOS < 11 +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-implementations" - (void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { if (status == kCLAuthorizationStatusNotDetermined) { return; @@ -115,6 +140,7 @@ - (void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStat [self cleanUp]; } +#pragma clang diagnostic pop - (void) cleanUp { self.locationManager = nil;