From a370de322a5fcab694a28875c912c84ac6a88ae4 Mon Sep 17 00:00:00 2001 From: Maurits van Beusekom Date: Mon, 25 Mar 2024 11:37:37 +0100 Subject: [PATCH 1/4] Adds support for when in use only --- permission_handler_apple/CHANGELOG.md | 9 +++++ permission_handler_apple/example/ios/Podfile | 5 +++ .../ios/Runner.xcodeproj/project.pbxproj | 20 +++++++++- .../xcshareddata/xcschemes/Runner.xcscheme | 2 +- .../ios/Classes/PermissionHandlerPlugin.m | 6 ++- .../ios/Classes/PermissionManager.h | 2 +- .../ios/Classes/PermissionManager.m | 14 ++++--- ...ppTrackingTransparencyPermissionStrategy.m | 2 +- .../strategies/AssistantPermissionStrategy.m | 2 +- .../strategies/AudioVideoPermissionStrategy.m | 2 +- .../strategies/BackgroundRefreshStrategy.m | 2 +- .../strategies/BluetoothPermissionStrategy.m | 2 +- .../strategies/ContactPermissionStrategy.m | 2 +- .../CriticalAlertsPermissionStrategy.m | 2 +- .../strategies/EventPermissionStrategy.m | 2 +- .../strategies/LocationPermissionStrategy.h | 2 +- .../strategies/LocationPermissionStrategy.m | 37 +++++++++++++++---- .../MediaLibraryPermissionStrategy.m | 2 +- .../NotificationPermissionStrategy.m | 2 +- .../Classes/strategies/PermissionStrategy.h | 3 +- .../strategies/PhonePermissionStrategy.m | 2 +- .../strategies/PhotoPermissionStrategy.m | 2 +- .../strategies/SensorPermissionStrategy.m | 2 +- .../strategies/SpeechPermissionStrategy.m | 2 +- .../strategies/StoragePermissionStrategy.m | 2 +- .../strategies/UnknownPermissionStrategy.m | 2 +- permission_handler_apple/pubspec.yaml | 2 +- 27 files changed, 97 insertions(+), 37 deletions(-) diff --git a/permission_handler_apple/CHANGELOG.md b/permission_handler_apple/CHANGELOG.md index 88e8b6f8b..2bad9a516 100644 --- a/permission_handler_apple/CHANGELOG.md +++ b/permission_handler_apple/CHANGELOG.md @@ -1,3 +1,12 @@ +## 9.4.3 + +* Adds the `PERMISSION_LOCATION_WHENINUSE` marco, which can be used instead of +the `PERMISSION_LOCATION` macro and only enable the `requestWhenInUseAuthorization` +and remove the `requestAlwaysAuthorization` when requesting location permission. +* Improves error handling when `Info.plist` doesn't contain the correct declarations. +* Adds support for the `NSLocationAlwaysAndWhenInUseUsageDescription` property list +key. + ## 9.4.2 * Updates the privacy manifest to include the use of the `NSUserDefaults` API. diff --git a/permission_handler_apple/example/ios/Podfile b/permission_handler_apple/example/ios/Podfile index 8c752e9e9..ffcc328e2 100644 --- a/permission_handler_apple/example/ios/Podfile +++ b/permission_handler_apple/example/ios/Podfile @@ -69,8 +69,13 @@ post_install do |installer| ## dart: PermissionGroup.photos 'PERMISSION_PHOTOS=1', + ## The 'PERMISSION_LOCATION' macro enables the `locationWhenInUse` and `locationAlways` permission. If + ## the application only requires `locationWhenInUse`, only specify the `PERMISSION_LOCATION_WHENINUSE` + ## macro. + ## ## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse] 'PERMISSION_LOCATION=1', + 'PERMISSION_LOCATION_WHENINUSE=0', ## dart: PermissionGroup.notification 'PERMISSION_NOTIFICATIONS=1', diff --git a/permission_handler_apple/example/ios/Runner.xcodeproj/project.pbxproj b/permission_handler_apple/example/ios/Runner.xcodeproj/project.pbxproj index 1f057e1e9..705be7680 100644 --- a/permission_handler_apple/example/ios/Runner.xcodeproj/project.pbxproj +++ b/permission_handler_apple/example/ios/Runner.xcodeproj/project.pbxproj @@ -141,6 +141,7 @@ 9705A1C41CF9048500538489 /* Embed Frameworks */, 3B06AD1E1E4923F5004D2608 /* Thin Binary */, D38B08CB85942E5D11545EE3 /* [CP] Embed Pods Frameworks */, + A7B07F67421488A414C73AAD /* [CP] Copy Pods Resources */, ); buildRules = ( ); @@ -157,7 +158,7 @@ 97C146E61CF9000F007C117D /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 1430; + LastUpgradeCheck = 1510; ORGANIZATIONNAME = ""; TargetAttributes = { 97C146ED1CF9000F007C117D = { @@ -252,6 +253,23 @@ shellPath = /bin/sh; shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" build"; }; + A7B07F67421488A414C73AAD /* [CP] Copy Pods Resources */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputFileListPaths = ( + "${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-resources-${CONFIGURATION}-input-files.xcfilelist", + ); + name = "[CP] Copy Pods Resources"; + outputFileListPaths = ( + "${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-resources-${CONFIGURATION}-output-files.xcfilelist", + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-resources.sh\"\n"; + showEnvVarsInLog = 0; + }; D38B08CB85942E5D11545EE3 /* [CP] Embed Pods Frameworks */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; diff --git a/permission_handler_apple/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme b/permission_handler_apple/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme index b52b2e698..e67b2808a 100644 --- a/permission_handler_apple/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme +++ b/permission_handler_apple/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme @@ -1,6 +1,6 @@ _methodResult = nil; - }]; + } errorHandler:^(NSString *errorCode, NSString *errorDescription) { + self->_methodResult([FlutterError errorWithCode:errorCode message:errorDescription details:nil]); + self->_methodResult = nil; + } +]; } else if ([@"shouldShowRequestPermissionRationale" isEqualToString:call.method]) { result(@false); diff --git a/permission_handler_apple/ios/Classes/PermissionManager.h b/permission_handler_apple/ios/Classes/PermissionManager.h index 4118ff83d..d929a6d66 100644 --- a/permission_handler_apple/ios/Classes/PermissionManager.h +++ b/permission_handler_apple/ios/Classes/PermissionManager.h @@ -35,7 +35,7 @@ typedef void (^PermissionRequestCompletion)(NSDictionary *permissionRequestResul @interface PermissionManager : NSObject - (instancetype)initWithStrategyInstances; -- (void)requestPermissions:(NSArray *)permissions completion:(PermissionRequestCompletion)completion; +- (void)requestPermissions:(NSArray *)permissions completion:(PermissionRequestCompletion)completion errorHandler:(PermissionErrorHandler)errorHandler; + (void)checkPermissionStatus:(enum PermissionGroup)permission result:(FlutterResult)result; + (void)checkServiceStatus:(enum PermissionGroup)permission result:(FlutterResult)result; diff --git a/permission_handler_apple/ios/Classes/PermissionManager.m b/permission_handler_apple/ios/Classes/PermissionManager.m index 7ce972f05..a6d708a8d 100644 --- a/permission_handler_apple/ios/Classes/PermissionManager.m +++ b/permission_handler_apple/ios/Classes/PermissionManager.m @@ -39,7 +39,7 @@ + (void)checkServiceStatus:(enum PermissionGroup)permission result:(FlutterResul }]; } -- (void)requestPermissions:(NSArray *)permissions completion:(PermissionRequestCompletion)completion { +- (void)requestPermissions:(NSArray *)permissions completion:(PermissionRequestCompletion)completion errorHandler:(PermissionErrorHandler)errorHandler { NSMutableDictionary *permissionStatusResult = [[NSMutableDictionary alloc] init]; if (permissions.count == 0) { @@ -57,22 +57,24 @@ - (void)requestPermissions:(NSArray *)permissions completion:(PermissionRequestC __block id permissionStrategy = [PermissionManager createPermissionStrategy:permission]; [_strategyInstances addObject:permissionStrategy]; - [permissionStrategy requestPermission:permission completionHandler:^(PermissionStatus permissionStatus) { permissionStatusResult[@(permission)] = @(permissionStatus); [requestQueue removeObject:@(permission)]; - + [self->_strategyInstances removeObject:permissionStrategy]; - + if (requestQueue.count == 0) { completion(permissionStatusResult); } - + // Make sure `completion` is called before cleaning up the reference // otherwise the `completion` block is also dereferenced on iOS 12 and // below (this is most likely a bug in Objective-C which is solved in // later versions of the runtime). permissionStrategy = nil; + } errorHandler: ^(NSString* errorCode, NSString* errorDesciption) { + errorHandler(errorCode, errorDesciption); + permissionStrategy = nil; }]; } } @@ -108,7 +110,7 @@ + (id)createPermissionStrategy:(PermissionGroup)permission { case PermissionGroupLocation: case PermissionGroupLocationAlways: case PermissionGroupLocationWhenInUse: - #if PERMISSION_LOCATION + #if PERMISSION_LOCATION || PERMISSION_LOCATION_WHENINUSE || PERMISSION_LOCATION_ALWAYS return [[LocationPermissionStrategy alloc] initWithLocationManager]; #else return [LocationPermissionStrategy new]; diff --git a/permission_handler_apple/ios/Classes/strategies/AppTrackingTransparencyPermissionStrategy.m b/permission_handler_apple/ios/Classes/strategies/AppTrackingTransparencyPermissionStrategy.m index 674345e94..11af3e17a 100644 --- a/permission_handler_apple/ios/Classes/strategies/AppTrackingTransparencyPermissionStrategy.m +++ b/permission_handler_apple/ios/Classes/strategies/AppTrackingTransparencyPermissionStrategy.m @@ -24,7 +24,7 @@ - (void)checkServiceStatus:(PermissionGroup)permission completionHandler:(Servic completionHandler(ServiceStatusNotApplicable); } -- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler { +- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler errorHandler:(PermissionErrorHandler)errorHandler{ PermissionStatus status = [self checkPermissionStatus:permission]; if (status != PermissionStatusDenied) { completionHandler(status); diff --git a/permission_handler_apple/ios/Classes/strategies/AssistantPermissionStrategy.m b/permission_handler_apple/ios/Classes/strategies/AssistantPermissionStrategy.m index a40af4290..377cc49f0 100644 --- a/permission_handler_apple/ios/Classes/strategies/AssistantPermissionStrategy.m +++ b/permission_handler_apple/ios/Classes/strategies/AssistantPermissionStrategy.m @@ -24,7 +24,7 @@ - (void)checkServiceStatus:(PermissionGroup)permission completionHandler:(Servic completionHandler(ServiceStatusNotApplicable); } -- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler { +- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler errorHandler:(PermissionErrorHandler)errorHandler { PermissionStatus status = [self checkPermissionStatus:permission]; if (status != PermissionStatusDenied) { completionHandler(status); diff --git a/permission_handler_apple/ios/Classes/strategies/AudioVideoPermissionStrategy.m b/permission_handler_apple/ios/Classes/strategies/AudioVideoPermissionStrategy.m index 0e71fca83..4226783ea 100644 --- a/permission_handler_apple/ios/Classes/strategies/AudioVideoPermissionStrategy.m +++ b/permission_handler_apple/ios/Classes/strategies/AudioVideoPermissionStrategy.m @@ -26,7 +26,7 @@ - (void)checkServiceStatus:(PermissionGroup)permission completionHandler:(Servic completionHandler(ServiceStatusNotApplicable); } -- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler { +- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler errorHandler:(PermissionErrorHandler)errorHandler { PermissionStatus status = [self checkPermissionStatus:permission]; if (status != PermissionStatusDenied) { diff --git a/permission_handler_apple/ios/Classes/strategies/BackgroundRefreshStrategy.m b/permission_handler_apple/ios/Classes/strategies/BackgroundRefreshStrategy.m index 6208ee85c..b72329514 100644 --- a/permission_handler_apple/ios/Classes/strategies/BackgroundRefreshStrategy.m +++ b/permission_handler_apple/ios/Classes/strategies/BackgroundRefreshStrategy.m @@ -17,7 +17,7 @@ - (void)checkServiceStatus:(PermissionGroup)permission completionHandler:(Servic completionHandler(ServiceStatusNotApplicable); } -- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler { +- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler errorHandler:(PermissionErrorHandler)errorHandler { completionHandler([BackgroundRefreshStrategy permissionStatus]); } diff --git a/permission_handler_apple/ios/Classes/strategies/BluetoothPermissionStrategy.m b/permission_handler_apple/ios/Classes/strategies/BluetoothPermissionStrategy.m index 24274f982..6443fe438 100644 --- a/permission_handler_apple/ios/Classes/strategies/BluetoothPermissionStrategy.m +++ b/permission_handler_apple/ios/Classes/strategies/BluetoothPermissionStrategy.m @@ -70,7 +70,7 @@ - (void)handleCheckServiceStatusCallback:(CBCentralManager *)centralManager { _serviceStatusHandler(serviceStatus); } -- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler { +- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler errorHandler:(PermissionErrorHandler)errorHandler { [self initManagerIfNeeded]; PermissionStatus status = [self checkPermissionStatus:permission]; diff --git a/permission_handler_apple/ios/Classes/strategies/ContactPermissionStrategy.m b/permission_handler_apple/ios/Classes/strategies/ContactPermissionStrategy.m index 1a6027b37..67b45b4ad 100644 --- a/permission_handler_apple/ios/Classes/strategies/ContactPermissionStrategy.m +++ b/permission_handler_apple/ios/Classes/strategies/ContactPermissionStrategy.m @@ -17,7 +17,7 @@ - (void)checkServiceStatus:(PermissionGroup)permission completionHandler:(Servic completionHandler(ServiceStatusNotApplicable); } -- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler { +- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler errorHandler:(PermissionErrorHandler)errorHandler { PermissionStatus status = [self checkPermissionStatus:permission]; if (status != PermissionStatusDenied) { diff --git a/permission_handler_apple/ios/Classes/strategies/CriticalAlertsPermissionStrategy.m b/permission_handler_apple/ios/Classes/strategies/CriticalAlertsPermissionStrategy.m index af0861d3f..180566d02 100644 --- a/permission_handler_apple/ios/Classes/strategies/CriticalAlertsPermissionStrategy.m +++ b/permission_handler_apple/ios/Classes/strategies/CriticalAlertsPermissionStrategy.m @@ -19,7 +19,7 @@ - (void)checkServiceStatus:(PermissionGroup)permission completionHandler:(Servic completionHandler(ServiceStatusNotApplicable); } -- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler { +- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler errorHandler:(PermissionErrorHandler)errorHandler { PermissionStatus status = [self checkPermissionStatus:permission]; if (status != PermissionStatusDenied) { completionHandler(status); diff --git a/permission_handler_apple/ios/Classes/strategies/EventPermissionStrategy.m b/permission_handler_apple/ios/Classes/strategies/EventPermissionStrategy.m index b41a725ff..80ac400ad 100644 --- a/permission_handler_apple/ios/Classes/strategies/EventPermissionStrategy.m +++ b/permission_handler_apple/ios/Classes/strategies/EventPermissionStrategy.m @@ -17,7 +17,7 @@ - (void)checkServiceStatus:(PermissionGroup)permission completionHandler:(Servic completionHandler(ServiceStatusNotApplicable); } -- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler { +- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler errorHandler:(PermissionErrorHandler)errorHandler { PermissionStatus permissionStatus = [self checkPermissionStatus:permission]; if (permissionStatus != PermissionStatusDenied) { diff --git a/permission_handler_apple/ios/Classes/strategies/LocationPermissionStrategy.h b/permission_handler_apple/ios/Classes/strategies/LocationPermissionStrategy.h index 9ca88c012..52bcc89dc 100644 --- a/permission_handler_apple/ios/Classes/strategies/LocationPermissionStrategy.h +++ b/permission_handler_apple/ios/Classes/strategies/LocationPermissionStrategy.h @@ -6,7 +6,7 @@ #import #import "PermissionStrategy.h" -#if PERMISSION_LOCATION +#if PERMISSION_LOCATION || PERMISSION_LOCATION_WHENINUSE || PERMISSION_LOCATION_ALWAYS #import diff --git a/permission_handler_apple/ios/Classes/strategies/LocationPermissionStrategy.m b/permission_handler_apple/ios/Classes/strategies/LocationPermissionStrategy.m index d34d9119a..9370f6343 100644 --- a/permission_handler_apple/ios/Classes/strategies/LocationPermissionStrategy.m +++ b/permission_handler_apple/ios/Classes/strategies/LocationPermissionStrategy.m @@ -5,7 +5,7 @@ #import "LocationPermissionStrategy.h" -#if PERMISSION_LOCATION +#if PERMISSION_LOCATION || PERMISSION_LOCATION_WHENINUSE || PERMISSION_LOCATION_ALWAYS NSString *const UserDefaultPermissionRequestedKey = @"org.baseflow.permission_handler_apple.permission_requested"; @@ -39,7 +39,7 @@ - (void)checkServiceStatus:(PermissionGroup)permission completionHandler:(Servic completionHandler([CLLocationManager locationServicesEnabled] ? ServiceStatusEnabled : ServiceStatusDisabled); } -- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler { +- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler errorHandler:(PermissionErrorHandler)errorHandler { PermissionStatus status = [self checkPermissionStatus:permission]; if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse && permission == PermissionGroupLocationAlways) { BOOL alreadyRequested = [[NSUserDefaults standardUserDefaults] boolForKey:UserDefaultPermissionRequestedKey]; // check if already requested the permantent permission @@ -56,26 +56,47 @@ - (void)requestPermission:(PermissionGroup)permission completionHandler:(Permiss _requestedPermission = permission; if (permission == PermissionGroupLocation) { - if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"] != nil) { +#if PERMISSION_LOCATION + bool hasAlwaysInInfoPlist = ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"] != nil || [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysAndWhenInUseUsageDescription"] != nil); + + if (hasAlwaysInInfoPlist && [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse) { [_locationManager requestAlwaysAuthorization]; } else if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"] != nil) { [_locationManager requestWhenInUseAuthorization]; } else { - [[NSException exceptionWithName:NSInternalInconsistencyException reason:@"To use location in iOS8 you need to define either NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription in the app bundle's Info.plist file" userInfo:nil] raise]; + errorHandler(@"MISSING_USAGE_DESCRIPTION", @"To use location in iOS8 you need to define at least NSLocationWhenInUseUsageDescription and optionally NSLocationAlwaysAndWhenInUseUsageDescription in the app bundle's Info.plist file"); + return; + } +#else + if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"] != nil ) { + [_locationManager requestWhenInUseAuthorization]; + } else { + errorHandler(@"MISSING_USAGE_DESCRIPTION", @"To use location in iOS8 you need to define at least NSLocationWhenInUseUsageDescription and optionally NSLocationAlwaysAndWhenInUseUsageDescription in the app bundle's Info.plist file"); + return; } +#endif } else if (permission == PermissionGroupLocationAlways) { - if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"] != nil) { +#if PERMISSION_LOCATION + if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) { + errorHandler(@"MISSING_WHENINUSE_PERMISSION", @"Must have \"When in use\" permission before it is allowed to request \"Always\" permission."); + return; + } + + if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"] != nil || [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysAndWhenInUseUsageDescription"] != nil ) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveActivityNotification:) name:UIApplicationDidBecomeActiveNotification object:nil]; [_locationManager requestAlwaysAuthorization]; [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:UserDefaultPermissionRequestedKey]; } else { - [[NSException exceptionWithName:NSInternalInconsistencyException reason:@"To use location in iOS8 you need to define NSLocationAlwaysUsageDescription in the app bundle's Info.plist file" userInfo:nil] raise]; + errorHandler(@"MISSING_USAGE_DESCRIPTION", @"To always use location in iOS8 you need to define at least NSLocationWhenInUseUsageDescription and optionally NSLocationAlwaysAndWhenInUseUsageDescription in the app bundle's Info.plist file"); + return; } +#endif } else if (permission == PermissionGroupLocationWhenInUse) { - if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"] != nil) { + if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"] != nil ) { [_locationManager requestWhenInUseAuthorization]; } else { - [[NSException exceptionWithName:NSInternalInconsistencyException reason:@"To use location in iOS8 you need to define NSLocationWhenInUseUsageDescription in the app bundle's Info.plist file" userInfo:nil] raise]; + errorHandler(@"MISSING_USAGE_DESCRIPTION", @"To use location in iOS8 you need to define at least NSLocationWhenInUseUsageDescription and optionally NSLocationAlwaysAndWhenInUseUsageDescription in the app bundle's Info.plist file"); + return; } } } diff --git a/permission_handler_apple/ios/Classes/strategies/MediaLibraryPermissionStrategy.m b/permission_handler_apple/ios/Classes/strategies/MediaLibraryPermissionStrategy.m index 19f990c3e..da8b24ef0 100644 --- a/permission_handler_apple/ios/Classes/strategies/MediaLibraryPermissionStrategy.m +++ b/permission_handler_apple/ios/Classes/strategies/MediaLibraryPermissionStrategy.m @@ -17,7 +17,7 @@ - (void)checkServiceStatus:(PermissionGroup)permission completionHandler:(Servic completionHandler(ServiceStatusNotApplicable); } -- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler { +- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler errorHandler:(PermissionErrorHandler)errorHandler { PermissionStatus status = [self checkPermissionStatus:permission]; if (status != PermissionStatusDenied) { completionHandler(status); diff --git a/permission_handler_apple/ios/Classes/strategies/NotificationPermissionStrategy.m b/permission_handler_apple/ios/Classes/strategies/NotificationPermissionStrategy.m index e002e93bc..41d4bcdda 100644 --- a/permission_handler_apple/ios/Classes/strategies/NotificationPermissionStrategy.m +++ b/permission_handler_apple/ios/Classes/strategies/NotificationPermissionStrategy.m @@ -19,7 +19,7 @@ - (void)checkServiceStatus:(PermissionGroup)permission completionHandler:(Servic completionHandler(ServiceStatusNotApplicable); } -- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler { +- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler errorHandler:(PermissionErrorHandler)errorHandler { PermissionStatus status = [self checkPermissionStatus:permission]; if (@available(iOS 12.0, *)) { if (status != PermissionStatusDenied && status != PermissionStatusProvisional) { diff --git a/permission_handler_apple/ios/Classes/strategies/PermissionStrategy.h b/permission_handler_apple/ios/Classes/strategies/PermissionStrategy.h index 995751cd5..eb03075b0 100644 --- a/permission_handler_apple/ios/Classes/strategies/PermissionStrategy.h +++ b/permission_handler_apple/ios/Classes/strategies/PermissionStrategy.h @@ -8,11 +8,12 @@ typedef void (^ServiceStatusHandler)(ServiceStatus serviceStatus); typedef void (^PermissionStatusHandler)(PermissionStatus permissionStatus); +typedef void (^PermissionErrorHandler)(NSString* errorCode, NSString* errorDescription); @protocol PermissionStrategy - (PermissionStatus)checkPermissionStatus:(PermissionGroup)permission; - (void)checkServiceStatus:(PermissionGroup)permission completionHandler:(ServiceStatusHandler)completionHandler; -- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler; +- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler errorHandler:(PermissionErrorHandler)errorHandler; @end diff --git a/permission_handler_apple/ios/Classes/strategies/PhonePermissionStrategy.m b/permission_handler_apple/ios/Classes/strategies/PhonePermissionStrategy.m index eff71a1f6..37222849d 100644 --- a/permission_handler_apple/ios/Classes/strategies/PhonePermissionStrategy.m +++ b/permission_handler_apple/ios/Classes/strategies/PhonePermissionStrategy.m @@ -24,7 +24,7 @@ - (void)checkServiceStatus:(PermissionGroup)permission completionHandler:(Servic completionHandler([self canDevicePlaceAPhoneCall] ? ServiceStatusEnabled : ServiceStatusDisabled); } -- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler { +- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler errorHandler:(PermissionErrorHandler)errorHandler { completionHandler(PermissionStatusPermanentlyDenied); } diff --git a/permission_handler_apple/ios/Classes/strategies/PhotoPermissionStrategy.m b/permission_handler_apple/ios/Classes/strategies/PhotoPermissionStrategy.m index f8118d835..a0cb5193d 100644 --- a/permission_handler_apple/ios/Classes/strategies/PhotoPermissionStrategy.m +++ b/permission_handler_apple/ios/Classes/strategies/PhotoPermissionStrategy.m @@ -28,7 +28,7 @@ - (void)checkServiceStatus:(PermissionGroup)permission completionHandler:(Servic completionHandler(ServiceStatusNotApplicable); } -- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler { +- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler errorHandler:(PermissionErrorHandler)errorHandler { PermissionStatus status = [self checkPermissionStatus:permission]; if (status != PermissionStatusDenied) { diff --git a/permission_handler_apple/ios/Classes/strategies/SensorPermissionStrategy.m b/permission_handler_apple/ios/Classes/strategies/SensorPermissionStrategy.m index 8304bd9ce..6cdf6c779 100644 --- a/permission_handler_apple/ios/Classes/strategies/SensorPermissionStrategy.m +++ b/permission_handler_apple/ios/Classes/strategies/SensorPermissionStrategy.m @@ -23,7 +23,7 @@ - (void)checkServiceStatus:(PermissionGroup)permission completionHandler:(Servic completionHandler(ServiceStatusDisabled); } -- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler { +- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler errorHandler:(PermissionErrorHandler)errorHandler { PermissionStatus status = [self checkPermissionStatus:permission]; if (status != PermissionStatusDenied) { diff --git a/permission_handler_apple/ios/Classes/strategies/SpeechPermissionStrategy.m b/permission_handler_apple/ios/Classes/strategies/SpeechPermissionStrategy.m index fc52785d5..cd178325c 100644 --- a/permission_handler_apple/ios/Classes/strategies/SpeechPermissionStrategy.m +++ b/permission_handler_apple/ios/Classes/strategies/SpeechPermissionStrategy.m @@ -16,7 +16,7 @@ - (void)checkServiceStatus:(PermissionGroup)permission completionHandler:(Servic completionHandler(ServiceStatusNotApplicable); } -- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler { +- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler errorHandler:(PermissionErrorHandler)errorHandler { PermissionStatus status = [self checkPermissionStatus:permission]; if (status != PermissionStatusDenied) { diff --git a/permission_handler_apple/ios/Classes/strategies/StoragePermissionStrategy.m b/permission_handler_apple/ios/Classes/strategies/StoragePermissionStrategy.m index 5c52595cc..2bc4f721a 100644 --- a/permission_handler_apple/ios/Classes/strategies/StoragePermissionStrategy.m +++ b/permission_handler_apple/ios/Classes/strategies/StoragePermissionStrategy.m @@ -17,7 +17,7 @@ - (void)checkServiceStatus:(PermissionGroup)permission completionHandler:(Servic completionHandler(ServiceStatusNotApplicable); } -- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler { +- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler errorHandler:(PermissionErrorHandler)errorHandler { completionHandler([StoragePermissionStrategy permissionStatus]); } diff --git a/permission_handler_apple/ios/Classes/strategies/UnknownPermissionStrategy.m b/permission_handler_apple/ios/Classes/strategies/UnknownPermissionStrategy.m index 01a716f3c..44bc6bc1e 100644 --- a/permission_handler_apple/ios/Classes/strategies/UnknownPermissionStrategy.m +++ b/permission_handler_apple/ios/Classes/strategies/UnknownPermissionStrategy.m @@ -16,7 +16,7 @@ - (void)checkServiceStatus:(PermissionGroup)permission completionHandler:(Servic completionHandler(ServiceStatusDisabled); } -- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler { +- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler errorHandler:(PermissionErrorHandler)errorHandler { completionHandler(PermissionStatusPermanentlyDenied); } @end diff --git a/permission_handler_apple/pubspec.yaml b/permission_handler_apple/pubspec.yaml index f7731dad5..a34cb011b 100644 --- a/permission_handler_apple/pubspec.yaml +++ b/permission_handler_apple/pubspec.yaml @@ -2,7 +2,7 @@ name: permission_handler_apple description: Permission plugin for Flutter. This plugin provides the iOS API to request and check permissions. repository: https://github.com/baseflow/flutter-permission-handler issue_tracker: https://github.com/Baseflow/flutter-permission-handler/issues -version: 9.4.2 +version: 9.4.3 environment: From 5ff4140b8f215016296143870c43abed12070d66 Mon Sep 17 00:00:00 2001 From: Maurits van Beusekom Date: Mon, 25 Mar 2024 17:02:02 +0100 Subject: [PATCH 2/4] Update permission_handler_apple/CHANGELOG.md Co-authored-by: TimHoogstrate --- permission_handler_apple/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/permission_handler_apple/CHANGELOG.md b/permission_handler_apple/CHANGELOG.md index 2bad9a516..05ba78593 100644 --- a/permission_handler_apple/CHANGELOG.md +++ b/permission_handler_apple/CHANGELOG.md @@ -1,6 +1,6 @@ ## 9.4.3 -* Adds the `PERMISSION_LOCATION_WHENINUSE` marco, which can be used instead of +* Adds the `PERMISSION_LOCATION_WHENINUSE` macro, which can be used instead of the `PERMISSION_LOCATION` macro and only enable the `requestWhenInUseAuthorization` and remove the `requestAlwaysAuthorization` when requesting location permission. * Improves error handling when `Info.plist` doesn't contain the correct declarations. From d0124ec9b716416eb98b2bc6c58defc1515a060c Mon Sep 17 00:00:00 2001 From: Maurits van Beusekom Date: Mon, 25 Mar 2024 17:02:16 +0100 Subject: [PATCH 3/4] Update permission_handler_apple/CHANGELOG.md Co-authored-by: TimHoogstrate --- permission_handler_apple/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/permission_handler_apple/CHANGELOG.md b/permission_handler_apple/CHANGELOG.md index 05ba78593..de9f1b791 100644 --- a/permission_handler_apple/CHANGELOG.md +++ b/permission_handler_apple/CHANGELOG.md @@ -1,7 +1,7 @@ ## 9.4.3 * Adds the `PERMISSION_LOCATION_WHENINUSE` macro, which can be used instead of -the `PERMISSION_LOCATION` macro and only enable the `requestWhenInUseAuthorization` +the `PERMISSION_LOCATION` macro, and exclusively enables the `requestWhenInUseAuthorization` and remove the `requestAlwaysAuthorization` when requesting location permission. * Improves error handling when `Info.plist` doesn't contain the correct declarations. * Adds support for the `NSLocationAlwaysAndWhenInUseUsageDescription` property list From c56d19090cd82beba06cb3aaf6de6a7fa15614d3 Mon Sep 17 00:00:00 2001 From: Maurits van Beusekom Date: Mon, 25 Mar 2024 17:05:57 +0100 Subject: [PATCH 4/4] Apply PR feedback --- .../ios/Classes/strategies/LocationPermissionStrategy.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/permission_handler_apple/ios/Classes/strategies/LocationPermissionStrategy.m b/permission_handler_apple/ios/Classes/strategies/LocationPermissionStrategy.m index 9370f6343..7e5b0b689 100644 --- a/permission_handler_apple/ios/Classes/strategies/LocationPermissionStrategy.m +++ b/permission_handler_apple/ios/Classes/strategies/LocationPermissionStrategy.m @@ -64,14 +64,14 @@ - (void)requestPermission:(PermissionGroup)permission completionHandler:(Permiss } else if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"] != nil) { [_locationManager requestWhenInUseAuthorization]; } else { - errorHandler(@"MISSING_USAGE_DESCRIPTION", @"To use location in iOS8 you need to define at least NSLocationWhenInUseUsageDescription and optionally NSLocationAlwaysAndWhenInUseUsageDescription in the app bundle's Info.plist file"); + errorHandler(@"MISSING_USAGE_DESCRIPTION", @"To use location from iOS8 you need to define at least NSLocationWhenInUseUsageDescription and optionally NSLocationAlwaysAndWhenInUseUsageDescription in the app bundle's Info.plist file"); return; } #else if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"] != nil ) { [_locationManager requestWhenInUseAuthorization]; } else { - errorHandler(@"MISSING_USAGE_DESCRIPTION", @"To use location in iOS8 you need to define at least NSLocationWhenInUseUsageDescription and optionally NSLocationAlwaysAndWhenInUseUsageDescription in the app bundle's Info.plist file"); + errorHandler(@"MISSING_USAGE_DESCRIPTION", @"To use location from iOS8 you need to define at least NSLocationWhenInUseUsageDescription and optionally NSLocationAlwaysAndWhenInUseUsageDescription in the app bundle's Info.plist file"); return; } #endif @@ -87,7 +87,7 @@ - (void)requestPermission:(PermissionGroup)permission completionHandler:(Permiss [_locationManager requestAlwaysAuthorization]; [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:UserDefaultPermissionRequestedKey]; } else { - errorHandler(@"MISSING_USAGE_DESCRIPTION", @"To always use location in iOS8 you need to define at least NSLocationWhenInUseUsageDescription and optionally NSLocationAlwaysAndWhenInUseUsageDescription in the app bundle's Info.plist file"); + errorHandler(@"MISSING_USAGE_DESCRIPTION", @"To always use location from iOS8 you need to define at least NSLocationWhenInUseUsageDescription and optionally NSLocationAlwaysAndWhenInUseUsageDescription in the app bundle's Info.plist file"); return; } #endif @@ -95,7 +95,7 @@ - (void)requestPermission:(PermissionGroup)permission completionHandler:(Permiss if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"] != nil ) { [_locationManager requestWhenInUseAuthorization]; } else { - errorHandler(@"MISSING_USAGE_DESCRIPTION", @"To use location in iOS8 you need to define at least NSLocationWhenInUseUsageDescription and optionally NSLocationAlwaysAndWhenInUseUsageDescription in the app bundle's Info.plist file"); + errorHandler(@"MISSING_USAGE_DESCRIPTION", @"To use location from iOS8 you need to define at least NSLocationWhenInUseUsageDescription and optionally NSLocationAlwaysAndWhenInUseUsageDescription in the app bundle's Info.plist file"); return; } }