Skip to content

Commit fb1f8b1

Browse files
Add ability to disable IDFA dialog on iOS
1 parent 1aa9b9c commit fb1f8b1

File tree

6 files changed

+43
-31
lines changed

6 files changed

+43
-31
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,11 @@ In debug mode SDK will log debugging information into console:
105105
```js
106106
UserReport.setDebug(true);
107107
```
108+
109+
#### IDFA and iOS 14
110+
111+
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:
112+
113+
```js
114+
UserReport.setIdfaDialog(false);
115+
```

demo/App.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ const App = () => {
2828
UserReport.setAnonymousTracking(dnt);
2929
}, [dnt]);
3030

31-
const [idfa, setIdfa] = useState(true);
32-
useEffect(() => {
33-
UserReport.setIdfa(idfa);
34-
}, [idfa]);
35-
3631
const renderRoot = () => (
3732
<>
3833
<Text style={styles.subheaderText}>Application</Text>
@@ -73,14 +68,6 @@ const App = () => {
7368
onValueChange={() => setDnt((previousValue) => !previousValue)}
7469
/>
7570
</View>
76-
77-
<View style={styles.switchView}>
78-
<Text style={styles.switchText}>IDFA</Text>
79-
<Switch
80-
value={idfa}
81-
onValueChange={() => setIdfa((previousValue) => !previousValue)}
82-
/>
83-
</View>
8471
</SafeAreaView>
8572
);
8673
};

ios/RNAdvertisingId.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ @interface RCT_EXTERN_MODULE(RNAdvertisingId, NSObject)
1010

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

13+
RCT_EXTERN_METHOD(getAdvertisingIdLegacy:(RCTPromiseResolveBlock)resolve rejecter: (RCTPromiseRejectBlock)reject)
14+
1315
+ (BOOL)requiresMainQueueSetup
1416
{
1517
return NO;

ios/RNAdvertisingId.swift

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import AdSupport
88
import AppTrackingTransparency
9-
//import Foundation
109

1110
@objc(RNAdvertisingId)
1211
class RNAdvertisingId: NSObject {
@@ -18,19 +17,18 @@ class RNAdvertisingId: NSObject {
1817
"isLimitAdTrackingEnabled" : !isAdvertisingTrackingEnabled,
1918
"advertisingId" : ""
2019
]
21-
20+
2221
if (isAdvertisingTrackingEnabled) {
2322
let idfa : String = ASIdentifierManager.shared().advertisingIdentifier.uuidString
2423
response["advertisingId"] = idfa
2524
}
26-
25+
2726
resolve(response)
2827
}
29-
30-
28+
29+
3130
}
3231

33-
//NEWLY ADDED PERMISSIONS FOR iOS 14
3432
func isAppTrackingEnabled(requestPermission: Bool = false, result: @escaping (_ isAuthorized: Bool) -> Void){
3533
if #available(iOS 14, *) {
3634
if(!requestPermission) {
@@ -51,4 +49,21 @@ class RNAdvertisingId: NSObject {
5149
result(ASIdentifierManager.shared().isAdvertisingTrackingEnabled);
5250
}
5351
}
52+
53+
@objc
54+
func getAdvertisingIdLegacy(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
55+
let isAdvertisingTrackingEnabled : Bool = ASIdentifierManager.shared().isAdvertisingTrackingEnabled
56+
57+
let response: NSMutableDictionary = [
58+
"isLimitAdTrackingEnabled" : !isAdvertisingTrackingEnabled,
59+
"advertisingId" : ""
60+
]
61+
62+
if (isAdvertisingTrackingEnabled) {
63+
let idfa : String = ASIdentifierManager.shared().advertisingIdentifier.uuidString
64+
response["advertisingId"] = idfa
65+
}
66+
67+
resolve(response)
68+
}
5469
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@audienceproject/react-native-userreport-sdk",
3-
"version": "3.0.1",
3+
"version": "3.0.2",
44
"description": "Running UserReport in React Native applications",
55
"main": "react-native-userreport-sdk.js",
66
"repository": {

react-native-userreport-sdk.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@ export const setAnonymousTracking = (value) => {
2626
debugInfo(`Anonymous tracking ${value ? 'enabled' : 'disabled'}`);
2727
};
2828

29-
/* idfa */
29+
/* idfa dialog */
3030

31-
let useIdfa = true;
31+
let useIdfaDialog = true;
3232

33-
export const setIdfa = (value) => {
34-
useIdfa = value;
35-
debugInfo(`IDFA ${value ? 'enabled' : 'disabled'}`);
33+
export const setIdfaDialog = (value) => {
34+
useIdfaDialog = value;
35+
debugInfo(`IDFA dialog ${value ? 'enabled' : 'disabled'}`);
3636
};
3737

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

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

103103
const path = `https://${domain}/hit.gif`;
104-
const params = `?t=${encodeURIComponent(trackingCode)}` // eslint-disable-line prefer-template
104+
const params = `?t=${encodeURIComponent(trackingCode)}`
105105
+ `&r=${random}`
106106
+ (!useAnonymousTracking && idfa ? `&d=${encodeURIComponent(idfa)}` : '')
107107
+ (!useAnonymousTracking && idfv ? `&idfv=${encodeURIComponent(idfv)}` : '')
@@ -139,7 +139,7 @@ export const trackSectionScreenView = async (sectionId) => {
139139
export default {
140140
setDebug,
141141
setAnonymousTracking,
142-
setIdfa,
142+
setIdfaDialog,
143143
configure,
144144
trackScreenView,
145145
trackSectionScreenView,

0 commit comments

Comments
 (0)