Skip to content

Commit

Permalink
Add ability to disable IDFA dialog on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayFrantsev committed Feb 2, 2021
1 parent 1aa9b9c commit fb1f8b1
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 31 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,11 @@ In debug mode SDK will log debugging information into console:
```js
UserReport.setDebug(true);
```

#### IDFA and iOS 14

Starting iOS 14.5 you’ll need to [receive user’s permission](https://developer.apple.com/app-store/user-privacy-and-data-use/) to access device advertising identifier. That behavior can be disabled for older iOS versions:

```js
UserReport.setIdfaDialog(false);
```
13 changes: 0 additions & 13 deletions demo/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ const App = () => {
UserReport.setAnonymousTracking(dnt);
}, [dnt]);

const [idfa, setIdfa] = useState(true);
useEffect(() => {
UserReport.setIdfa(idfa);
}, [idfa]);

const renderRoot = () => (
<>
<Text style={styles.subheaderText}>Application</Text>
Expand Down Expand Up @@ -73,14 +68,6 @@ const App = () => {
onValueChange={() => setDnt((previousValue) => !previousValue)}
/>
</View>

<View style={styles.switchView}>
<Text style={styles.switchText}>IDFA</Text>
<Switch
value={idfa}
onValueChange={() => setIdfa((previousValue) => !previousValue)}
/>
</View>
</SafeAreaView>
);
};
Expand Down
2 changes: 2 additions & 0 deletions ios/RNAdvertisingId.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ @interface RCT_EXTERN_MODULE(RNAdvertisingId, NSObject)

RCT_EXTERN_METHOD(getAdvertisingId:(RCTPromiseResolveBlock)resolve rejecter: (RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(getAdvertisingIdLegacy:(RCTPromiseResolveBlock)resolve rejecter: (RCTPromiseRejectBlock)reject)

+ (BOOL)requiresMainQueueSetup
{
return NO;
Expand Down
27 changes: 21 additions & 6 deletions ios/RNAdvertisingId.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import AdSupport
import AppTrackingTransparency
//import Foundation

@objc(RNAdvertisingId)
class RNAdvertisingId: NSObject {
Expand All @@ -18,19 +17,18 @@ class RNAdvertisingId: NSObject {
"isLimitAdTrackingEnabled" : !isAdvertisingTrackingEnabled,
"advertisingId" : ""
]

if (isAdvertisingTrackingEnabled) {
let idfa : String = ASIdentifierManager.shared().advertisingIdentifier.uuidString
response["advertisingId"] = idfa
}

resolve(response)
}


}

//NEWLY ADDED PERMISSIONS FOR iOS 14
func isAppTrackingEnabled(requestPermission: Bool = false, result: @escaping (_ isAuthorized: Bool) -> Void){
if #available(iOS 14, *) {
if(!requestPermission) {
Expand All @@ -51,4 +49,21 @@ class RNAdvertisingId: NSObject {
result(ASIdentifierManager.shared().isAdvertisingTrackingEnabled);
}
}

@objc
func getAdvertisingIdLegacy(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
let isAdvertisingTrackingEnabled : Bool = ASIdentifierManager.shared().isAdvertisingTrackingEnabled

let response: NSMutableDictionary = [
"isLimitAdTrackingEnabled" : !isAdvertisingTrackingEnabled,
"advertisingId" : ""
]

if (isAdvertisingTrackingEnabled) {
let idfa : String = ASIdentifierManager.shared().advertisingIdentifier.uuidString
response["advertisingId"] = idfa
}

resolve(response)
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@audienceproject/react-native-userreport-sdk",
"version": "3.0.1",
"version": "3.0.2",
"description": "Running UserReport in React Native applications",
"main": "react-native-userreport-sdk.js",
"repository": {
Expand Down
22 changes: 11 additions & 11 deletions react-native-userreport-sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ export const setAnonymousTracking = (value) => {
debugInfo(`Anonymous tracking ${value ? 'enabled' : 'disabled'}`);
};

/* idfa */
/* idfa dialog */

let useIdfa = true;
let useIdfaDialog = true;

export const setIdfa = (value) => {
useIdfa = value;
debugInfo(`IDFA ${value ? 'enabled' : 'disabled'}`);
export const setIdfaDialog = (value) => {
useIdfaDialog = value;
debugInfo(`IDFA dialog ${value ? 'enabled' : 'disabled'}`);
};

const loadIdfa = async () => {
if (!useIdfa) {
return '';
}
const nativeModule = ReactNative.NativeModules.RNAdvertisingId;
const nativeMethod = ReactNative.Platform.OS !=='ios' || useIdfaDialog
? nativeModule.getAdvertisingId : nativeModule.getAdvertisingIdLegacy;

try {
const data = await ReactNative.NativeModules.RNAdvertisingId.getAdvertisingId(); // FIXME: https://github.com/applike/react-native-advertising-id/pull/26
const data = await nativeMethod();
return data.advertisingId || '';
} catch (error) {
return '';
Expand Down Expand Up @@ -101,7 +101,7 @@ const fireTrackingPixel = async (trackingCode, consentString) => {
const deviceResolution = `${screenWidth}x${screenHeight}`;

const path = `https://${domain}/hit.gif`;
const params = `?t=${encodeURIComponent(trackingCode)}` // eslint-disable-line prefer-template
const params = `?t=${encodeURIComponent(trackingCode)}`
+ `&r=${random}`
+ (!useAnonymousTracking && idfa ? `&d=${encodeURIComponent(idfa)}` : '')
+ (!useAnonymousTracking && idfv ? `&idfv=${encodeURIComponent(idfv)}` : '')
Expand Down Expand Up @@ -139,7 +139,7 @@ export const trackSectionScreenView = async (sectionId) => {
export default {
setDebug,
setAnonymousTracking,
setIdfa,
setIdfaDialog,
configure,
trackScreenView,
trackSectionScreenView,
Expand Down

0 comments on commit fb1f8b1

Please sign in to comment.