Skip to content

Commit 6ca0290

Browse files
yangyuchun (C)yangyuchunxa
authored andcommitted
feat:适配harmony平台
1 parent 6679e9f commit 6ca0290

File tree

17 files changed

+284
-20566
lines changed

17 files changed

+284
-20566
lines changed

README.md

Lines changed: 9 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,17 @@
1-
@sparkfabrik/react-native-idfa-aaid
1+
# react-native-idfa-aaid
22

3-
# React Native module to get IDFA (iOS) or AAID (Android)
3+
本项目基于 [react-native-idfa-aaid](https://github.com/sparkfabrik/sparkfabrik-react-native-idfa-aaid)
44

5-
## Intro
5+
## 文档地址 / Documentation URL
66

7-
[React Native](https://reactnative.dev/) is a framework for creating native mobile apps based on React.
7+
[中文 / Chinese](https://gitee.com/react-native-oh-library/usage-docs/blob/master/zh-cn/react-native-idfa-aaid.md)
88

9-
The **Advertising Identifier** ([IDFA](https://developer.apple.com/documentation/adsupport/asidentifiermanager) on iOS, [AAID](https://developer.android.com/training/articles/ad-id) on Android) is a device-specific, unique, resettable ID for advertising that allows developers and marketers to track activity for advertising purposes.
9+
## Codegen
1010

11-
This npm module allows any mobile application built with React Native to access the Advertising ID, following the OS specific definition and user permissions.
11+
该库已接入 codegen,具体请查阅文档。
1212

13-
The module output in the RN framework is the following:
13+
The library has been integrated with codegen. Please refer to the documentation for details.
1414

15-
```ts
16-
interface AdvertisingInfoResponse {
17-
id: string; // the Advertising ID (or null if not defined/permitted)
18-
isAdTrackingLimited: boolean; // the user defined permission to track
19-
}
20-
```
15+
## 请悉知 / Acknowledgements
2116

22-
## Supported platform
23-
24-
- Android
25-
- iOS
26-
27-
## Installation
28-
29-
```sh
30-
npm install @sparkfabrik/react-native-idfa-aaid
31-
```
32-
33-
or
34-
35-
```sh
36-
yarn add @sparkfabrik/react-native-idfa-aaid
37-
```
38-
39-
Then run `pod install` in your `ios` folder after installation.
40-
41-
## Usage
42-
43-
### iOS configuration
44-
45-
For `native` apps, in `info.plist` make sure to add:
46-
47-
```xml
48-
<key>NSUserTrackingUsageDescription</key>
49-
<string>...</string>
50-
```
51-
52-
For `Expo` apps, in `app.json` make sure to add:
53-
54-
```json
55-
{
56-
"expo": {
57-
"plugins": [
58-
[
59-
"expo-tracking-transparency",
60-
{
61-
"userTrackingPermission": "..."
62-
}
63-
]
64-
]
65-
}
66-
}
67-
```
68-
69-
### React Native components
70-
71-
Example of a basic integration in a RN component.
72-
73-
```js
74-
import ReactNativeIdfaAaid, { AdvertisingInfoResponse } from '@sparkfabrik/react-native-idfa-aaid';
75-
76-
const MyComponent: React.FC = () => {
77-
const [idfa, setIdfa] = useState<string | null>();
78-
79-
useEffect(() => {
80-
ReactNativeIdfaAaid.getAdvertisingInfo()
81-
.then((res: AdvertisingInfoResponse) =>
82-
!res.isAdTrackingLimited ? setIdfa(res.id) : setIdfa(null),
83-
)
84-
.catch((err) => {
85-
console.log(err);
86-
return setIdfa(null);
87-
});
88-
}, []);
89-
```
90-
91-
#### iOS 17.4
92-
93-
In order to prevent a bug present in iOS 17.4 we also expose the `getAdvertisingInfoAndCheckAuthorization(check: boolean)` which aims to solve the problem of `ATT Tracking Manager` returning status `denied` even if the ATT modal was not yet displayed to the user.
94-
95-
```js
96-
import ReactNativeIdfaAaid, { AdvertisingInfoResponse } from '@sparkfabrik/react-native-idfa-aaid';
97-
98-
const MyComponent: React.FC = () => {
99-
const [idfa, setIdfa] = useState<string | null>();
100-
101-
useEffect(() => {
102-
ReactNativeIdfaAaid.getAdvertisingInfoAndCheckAuthorization(true)
103-
.then((res: AdvertisingInfoResponse) =>
104-
!res.isAdTrackingLimited ? setIdfa(res.id) : setIdfa(null),
105-
)
106-
.catch((err) => {
107-
console.log(err);
108-
return setIdfa(null);
109-
});
110-
}, []);
111-
```
112-
113-
## Contributing
114-
115-
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
116-
117-
## License
118-
119-
MIT
17+
本项目基于 [The MIT License (MIT)](https://github.com/sparkfabrik/sparkfabrik-react-native-idfa-aaid/blob/master/LICENSE) ,请自由地享受和参与开源。

harmony/getOaid.har

4.39 KB
Binary file not shown.

harmony/getOaid/Index.ets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./ts";

harmony/getOaid/build-profile.json5

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"apiType": "stageMode",
3+
"buildOption": {
4+
},
5+
"buildOptionSet": [
6+
{
7+
"name": "release",
8+
"arkOptions": {
9+
"obfuscation": {
10+
"ruleOptions": {
11+
"enable": true,
12+
"files": [
13+
"./obfuscation-rules.txt"
14+
]
15+
},
16+
}
17+
},
18+
},
19+
],
20+
"targets": [
21+
{
22+
"name": "default"
23+
},
24+
{
25+
"name": "ohosTest"
26+
}
27+
]
28+
}

harmony/getOaid/hvigorfile.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { harTasks } from '@ohos/hvigor-ohos-plugin';
2+
3+
export default {
4+
system: harTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
5+
plugins:[] /* Custom plugin to extend the functionality of Hvigor. */
6+
}

harmony/getOaid/obfuscation-rules.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Define project specific obfuscation rules here.
2+
# You can include the obfuscation configuration files in the current module's build-profile.json5.
3+
#
4+
# For more details, see
5+
# https://gitee.com/openharmony/arkcompiler_ets_frontend/blob/master/arkguard/README.md
6+
7+
# Obfuscation options:
8+
# -disable-obfuscation: disable all obfuscations
9+
# -enable-property-obfuscation: obfuscate the property names
10+
# -enable-toplevel-obfuscation: obfuscate the names in the global scope
11+
# -compact: remove unnecessary blank spaces and all line feeds
12+
# -remove-log: remove all console.* statements
13+
# -print-namecache: print the name cache that contains the mapping from the old names to new names
14+
# -apply-namecache: reuse the given cache file
15+
16+
# Keep options:
17+
# -keep-property-name: specifies property names that you want to keep
18+
# -keep-global-name: specifies names that you want to keep in the global scope

harmony/getOaid/oh-package.json5

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "@react-native-oh-tpl/react-native-idfa-aaid",
3+
"version": "1.2.0-0.0.1",
4+
"description": "",
5+
"main": "Index.ets",
6+
"license": "Apache-2.0",
7+
"dependencies": {
8+
"@rnoh/react-native-openharmony": 'file:../react_native_openharmony',
9+
}
10+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { TurboModule } from '@rnoh/react-native-openharmony/ts';
2+
import { TM } from "@rnoh/react-native-openharmony/generated/ts"
3+
import { abilityAccessCtrl, Permissions, bundleManager, common } from '@kit.AbilityKit';
4+
import { identifier } from '@kit.AdsKit';
5+
6+
export type AdvertisingInfoResponse = {
7+
id: string | null;
8+
isAdTrackingLimited: boolean;
9+
};
10+
11+
const permission = 'ohos.permission.APP_TRACKING_CONSENT';
12+
13+
async function getAccessTokenID(): Promise<number> {
14+
try {
15+
const bundleInfo: bundleManager.BundleInfo =
16+
await bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
17+
const appInfo: bundleManager.ApplicationInfo = bundleInfo.appInfo;
18+
return appInfo.accessTokenId;
19+
} catch (error) {
20+
throw new Error(`Error getting AccessToken ID: ${error}`);
21+
}
22+
}
23+
24+
async function checkPermissionGrant(tokenId: number, permission: Permissions): Promise<abilityAccessCtrl.GrantStatus> {
25+
const atManager = abilityAccessCtrl.createAtManager();
26+
return await atManager.checkAccessToken(tokenId, permission);
27+
}
28+
29+
async function requestPermission(context: common.UIAbilityContext, permission: Permissions): Promise<void> {
30+
const atManager = abilityAccessCtrl.createAtManager();
31+
await atManager.requestPermissionsFromUser(context, [permission]);
32+
}
33+
34+
async function obtainingPermissions(tokenId: number): Promise<string | null> {
35+
try {
36+
const oaid = await identifier.getOAID();
37+
return oaid;
38+
} catch (error) {
39+
console.error('Error retrieving OAID:', error);
40+
return null;
41+
}
42+
}
43+
44+
async function ModuleImplementation(context): Promise<AdvertisingInfoResponse> {
45+
let result: AdvertisingInfoResponse = { id: null, isAdTrackingLimited: true };
46+
try {
47+
const tokenId = await getAccessTokenID();
48+
let isAdTrackingLimited = false;
49+
50+
const permissionStatus = await checkPermissionGrant(tokenId, permission);
51+
if (permissionStatus !== abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED) {
52+
try {
53+
await requestPermission(context, permission);
54+
const newPermissionStatus = await checkPermissionGrant(tokenId, permission);
55+
if (newPermissionStatus !== abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED) {
56+
isAdTrackingLimited = true;
57+
}
58+
} catch (error) {
59+
console.error('Permission request failed:', error);
60+
isAdTrackingLimited = true;
61+
}
62+
}
63+
const oaid = await obtainingPermissions(tokenId);
64+
result.id = oaid;
65+
result.isAdTrackingLimited = isAdTrackingLimited;
66+
return result;
67+
} catch (error) {
68+
console.error('Error getting OAID with permission:', error);
69+
return result;
70+
}
71+
}
72+
73+
export class GetOaidModule extends TurboModule implements TM.RTNGetOaidNativeModule.Spec {
74+
private context: common.UIAbilityContext = this.ctx.uiAbilityContext;
75+
76+
async getAdvertisingInfo(): Promise<AdvertisingInfoResponse> {
77+
return ModuleImplementation(this.context);
78+
}
79+
80+
async getAdvertisingInfoAndCheckAuthorization(check: boolean): Promise<AdvertisingInfoResponse> {
81+
return ModuleImplementation(this.context);
82+
}
83+
}
84+
85+
86+
87+
88+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {
2+
RNPackage,
3+
TurboModulesFactory,
4+
} from "@rnoh/react-native-openharmony/ts";
5+
import type {
6+
TurboModule,
7+
TurboModuleContext,
8+
} from "@rnoh/react-native-openharmony/ts";
9+
import { TM } from "@rnoh/react-native-openharmony/generated/ts";
10+
import { GetOaidModule } from './GetOaidModule';
11+
12+
class GetOaidModulesFactory extends TurboModulesFactory {
13+
createTurboModule(name: string): TurboModule | null {
14+
if (name === TM.RTNGetOaidNativeModule.NAME) {
15+
return new GetOaidModule(this.ctx);
16+
}
17+
return null;
18+
}
19+
20+
hasTurboModule(name: string): boolean {
21+
return name === TM.RTNGetOaidNativeModule.NAME;
22+
}
23+
}
24+
25+
export class GetOaidPackage extends RNPackage {
26+
createTurboModulesFactory(ctx: TurboModuleContext): TurboModulesFactory {
27+
return new GetOaidModulesFactory(ctx);
28+
}
29+
}

harmony/getOaid/src/main/module.json5

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
{
3+
"module" : {
4+
"name": 'getOaid',
5+
"type": 'har',
6+
"deviceTypes": ['default'],
7+
"requestPermissions":[
8+
{
9+
"name" : "ohos.permission.APP_TRACKING_CONSENT",
10+
"reason": "$string:page_show",
11+
"usedScene": {
12+
"abilities": [
13+
"FormAbility"
14+
],
15+
"when":"always"
16+
},
17+
},
18+
]
19+
}
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"string": [
3+
{
4+
"name": "module_desc",
5+
"value": "module description"
6+
},
7+
{
8+
"name": "EntryAbility_desc",
9+
"value": "description"
10+
},
11+
{
12+
"name": "EntryAbility_label",
13+
"value": "label"
14+
}
15+
]
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"string": [
3+
{
4+
"name": "module_desc",
5+
"value": "模块描述"
6+
},
7+
{
8+
"name": "EntryAbility_desc",
9+
"value": "description"
10+
},
11+
{
12+
"name": "EntryAbility_label",
13+
"value": "label"
14+
}
15+
]
16+
}

harmony/getOaid/ts.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./src/main/ets/GetOaidPackage";
2+
export * from "./src/main/ets/GetOaidModule";

0 commit comments

Comments
 (0)