Ave*_*235 15 android react-native expo
此库允许您获取Android设备的唯一设备ID/Mac地址,重新安装后不会更改.
Expo.Constants.deviceId 每次重新安装后更改(即使应用程序版本号相同).
有没有办法获得一个Android的唯一ID,重新安装后不会改变(至少如果它是相同的版本),而不弹出?
Mor*_*s S 29
对于 Expo IOS,目前的选项非常有限,因为 Apple 禁止获取私人设备信息。我们需要在下面创建我们自己的唯一标识符。
解决方案:
我的解决方案是uuid和 Expo SecureStore的组合,适用于 IOS 和 Android。
import * as SecureStore from 'expo-secure-store';
import 'react-native-get-random-values';
import { v4 as uuidv4 } from 'uuid';
let uuid = uuidv4();
await SecureStore.setItemAsync('secure_deviceid', JSON.stringify(uuid));
let fetchUUID = await SecureStore.getItemAsync('secure_deviceid');
console.log(fetchUUID)
Run Code Online (Sandbox Code Playgroud)
即使重新安装应用程序,或者用户切换设备并将所有数据复制到新设备,此解决方案也将起作用。(Expo.Constants.deviceId已弃用,并将在 Expo SDK 44 中删除)。
完整示例:
检查您是否已将uuid存储在SecureStore中
import * as SecureStore from 'expo-secure-store';
import 'react-native-get-random-values';
import { v4 as uuidv4 } from 'uuid';
let uuid = uuidv4();
let fetchUUID = await SecureStore.getItemAsync('secure_deviceid');
//if user has already signed up prior
if (fetchUUID) {
uuid = fetchUUID
}
await SecureStore.setItemAsync('secure_deviceid', JSON.stringify(uuid));
console.log(uuid)
Run Code Online (Sandbox Code Playgroud)
Application.androidId从博览会应用程序中使用。重新安装或更新后,ID 不会更改。如果在设备上执行恢复出厂设置或 APK 签名密钥发生更改,则该值可能会更改。
https://docs.expo.dev/versions/latest/sdk/application/#applicationandroidid
例子:
import * as Application from 'expo-application';
import { Platform } from 'expo-modules-core';
import * as SecureStore from 'expo-secure-store';
import Constants from 'expo-constants';
const getDeviceId = async () => {
if (Platform.OS === 'android') {
return Application.androidId;
} else {
let deviceId = await SecureStore.getItemAsync('deviceId');
if (!deviceId) {
deviceId = Constants.deviceId; //or generate uuid
await SecureStore.setItemAsync('deviceId', deviceId);
}
return deviceId;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 0
我猜你可以使用 facebook 模块来达到这个目的。 https://docs.expo.io/versions/latest/sdk/facebook-ads/#currentdevicehash
不确定幕后发生了什么 - 但看起来它在应用程序重新安装、设备重新启动等之间是独一无二的。
| 归档时间: |
|
| 查看次数: |
1377 次 |
| 最近记录: |