这个库可以让你获取 Android 设备的唯一设备 ID / Mac 地址,在应用重新安装后不会改变。
Expo.Constants.deviceId
在每次重新安装后都会改变(即使应用程序版本号相同)。
有没有办法在不弹出的情况下获得 Android 的唯一 ID,而在重新安装后不改变(至少对于相同的版本)?
这个库可以让你获取 Android 设备的唯一设备 ID / Mac 地址,在应用重新安装后不会改变。
Expo.Constants.deviceId
在每次重新安装后都会改变(即使应用程序版本号相同)。
有没有办法在不弹出的情况下获得 Android 的唯一 ID,而在重新安装后不改变(至少对于相同的版本)?
对于Expo IOS,目前的选择非常有限,因为苹果禁止获取私人设备信息。我们需要在下面创建自己的唯一标识符。
解决方案:
我的解决方案是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)
即使应用程序被重新安装,或者用户更换设备并将所有数据复制到新设备上,此解决方案也可以正常工作。
(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)
uuid
每次调用都会给出一个新的唯一标识符。
对于您的情况,您需要先检查是否已经有 uuid 并从 SecureStore
中检索。 - Morris S使用来自 expo-application 的 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;
}
}
最佳方法是在Android中使用ANDROID_ID
,在iOS中使用IDFV
。
对于Expo,这是一个包:https://docs.expo.dev/versions/latest/sdk/application/#applicationgetiosidforvendorasync
我的代码:
const newUniqueId = Platform.OS === 'android'
? Application.androidId ?? uuid.v4().toString()
: await Application.getIosIdForVendorAsync() ?? uuid.v4().toString();
let uniqueId = null;
try {
uniqueId = await SecureStore.getItemAsync('uniqueId');
} catch (error) {
LoggerService.logToSentry(error);
}
try {
if (!uniqueId) {
uniqueId = newUniqueId;
await SecureStore.setItemAsync('uniqueId', uniqueId);
}
} catch (error) {
uniqueId = newUniqueId;
LoggerService.logToSentry(error);
}
return uniqueId;
uuid.v4()
并在使用SecureStore
存储后删除应用程序,除了iOS
手机外,所有数据都将被删除。在Android上,所有App
数据将与存储的密钥一起被删除。猜测您可以使用Facebook模块来实现此目的。 https://docs.expo.io/versions/latest/sdk/facebook-ads/#currentdevicehash
不确定在幕后发生了什么 - 但看起来它在应用程序重新安装、设备重启等情况下是唯一的。
只需使用react-native-device-info中的getuniqueid()方法。适用于iOS和Android以唯一标识设备。
react-native-device-info
通常不会与 Expo 兼容,除非您使用的是 Expo 裸应用程序。 - Morris S