diff --git a/permission_handler_apple/CHANGELOG.md b/permission_handler_apple/CHANGELOG.md index fbd51929f..286acdaf6 100644 --- a/permission_handler_apple/CHANGELOG.md +++ b/permission_handler_apple/CHANGELOG.md @@ -1,3 +1,7 @@ +## 9.4.8 + +* Fixed iOS permission request state not being reset when app enters background, causing subsequent requests to fail with `ERROR_ALREADY_REQUESTING_PERMISSIONS`. + ## 9.4.7 * Increases minimum supported Flutter version to 3.3.0, and removes code only diff --git a/permission_handler_apple/ios/Classes/PermissionHandlerPlugin.m b/permission_handler_apple/ios/Classes/PermissionHandlerPlugin.m index cfbad93cb..ca9a421de 100644 --- a/permission_handler_apple/ios/Classes/PermissionHandlerPlugin.m +++ b/permission_handler_apple/ios/Classes/PermissionHandlerPlugin.m @@ -21,6 +21,13 @@ + (void)registerWithRegistrar:(NSObject *)registrar { binaryMessenger:[registrar messenger]]; PermissionManager *permissionManager = [[PermissionManager alloc] initWithStrategyInstances]; PermissionHandlerPlugin *instance = [[PermissionHandlerPlugin alloc] initWithPermissionManager:permissionManager]; + + [[NSNotificationCenter defaultCenter] + addObserver:instance + selector:@selector(handleApplicationDidEnterBackground:) + name:UIApplicationDidEnterBackgroundNotification + object:nil]; + [registrar addMethodCallDelegate:instance channel:channel]; } @@ -62,4 +69,23 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result } } +- (void)handleApplicationDidEnterBackground:(NSNotification *)notification { + if (_methodResult != nil) { + #ifdef DEBUG + NSLog(@"[PermissionHandler] App backgrounded with pending permission request. Cancelling request."); + #endif + + _methodResult([FlutterError + errorWithCode:@"REQUEST_INTERRUPTED" + message:@"Permission request was interrupted because the app entered background" + details:nil]); + + _methodResult = nil; + } +} + +- (void)dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; +} + @end