Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only request App Tracking Transparency when app is active #751

Merged
merged 1 commit into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -955,26 +955,6 @@ When requesting `PERMISSIONS.IOS.LOCATION_ALWAYS`, if the user choose `Allow Whi

Subsequently, if you are requesting `LOCATION_ALWAYS` permission, there is no need to request `LOCATION_WHEN_IN_USE`. If the user accepts, `LOCATION_WHEN_IN_USE` will be granted too. If the user denies, `LOCATION_WHEN_IN_USE` will be denied too.

### How to request "App Tracking Transparency" permission on iOS

Since iOS 15.0, it's impossible to request this permission if the app isn't `active` (see [#648](https://github.com/zoontek/react-native-permissions/issues/648)). A good solution is to use `AppState` to make sure this is the case:

```js
useEffect(() => {
const callback = (status: AppStateStatus) => {
if (status === 'active') {
request(PERMISSIONS.IOS.APP_TRACKING_TRANSPARENCY)
.then((result) => console.log(result))
.catch((error) => console.log(error));
}
};

callback(AppState.currentState); // initial call
const listener = AppState.addEventListener('change', callback);
return listener.remove;
}, []);
```

### Testing with Jest

If you don't already have a Jest setup file configured, please add the following to your Jest configuration file and create the new `jest.setup.js` file in project root:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
@import AppTrackingTransparency;
@import AdSupport;

@interface RNPermissionHandlerAppTrackingTransparency()

@property (nonatomic, strong) void (^resolve)(RNPermissionStatus status);
@property (nonatomic, strong) void (^reject)(NSError *error);

@end

@implementation RNPermissionHandlerAppTrackingTransparency

+ (NSArray<NSString *> * _Nonnull)usageDescriptionKeys {
Expand Down Expand Up @@ -38,12 +45,38 @@ - (void)checkWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
- (void)requestWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
rejecter:(void (^ _Nonnull)(NSError * _Nonnull))reject {
if (@available(iOS 14.0, *)) {
[ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(__unused ATTrackingManagerAuthorizationStatus status) {
[self checkWithResolver:resolve rejecter:reject];
}];
if ([ATTrackingManager trackingAuthorizationStatus] != ATTrackingManagerAuthorizationStatusNotDetermined) {
return [self checkWithResolver:resolve rejecter:reject];
}

if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive) {
[ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(__unused ATTrackingManagerAuthorizationStatus status) {
[self checkWithResolver:resolve rejecter:reject];
}];
} else {
_resolve = resolve;
_reject = reject;

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onApplicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
} else {
[self checkWithResolver:resolve rejecter:reject];
}
}

- (void)onApplicationDidBecomeActive:(__unused NSNotification *)notification {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidBecomeActiveNotification
object:nil];

if (@available(iOS 14.0, *)) {
[ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(__unused ATTrackingManagerAuthorizationStatus status) {
[self checkWithResolver:self->_resolve rejecter:self->_reject];
}];
}
}

@end
4 changes: 2 additions & 2 deletions ios/FaceID/RNPermissionHandlerFaceID.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ - (void)requestWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
_laContext = context;

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(UIApplicationDidBecomeActiveNotification:)
selector:@selector(onApplicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];

Expand All @@ -95,7 +95,7 @@ - (void)invalidateContext {
[_laContext invalidate];
}

- (void)UIApplicationDidBecomeActiveNotification:(__unused NSNotification *)notification {
- (void)onApplicationDidBecomeActive:(__unused NSNotification *)notification {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidBecomeActiveNotification
object:nil];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-permissions",
"version": "3.7.0",
"version": "3.7.1",
"license": "MIT",
"description": "An unified permissions API for React Native on iOS, Android and Windows",
"author": "Mathieu Acthernoene <zoontek@gmail.com>",
Expand Down