问题描述
这个库允许你获取唯一的设备 ID/Mac 地址Android 设备,重新安装后不会改变.
This library allows you to get unique device id / Mac address of Android devices, which doesn't change after reinstallation.
Expo.Constants.deviceId
在每次重新安装后都会发生变化(即使应用版本号相同).
Expo.Constants.deviceId
changes after every reinstallation (even if the app version number is the same).
有没有办法在不弹出的情况下为 Android 获取一个在重新安装后不会改变的唯一 ID(至少对于相同的版本)?
Is there a way to get an unique id for Android that doesn't change after reinstallation (at least for if it's the same version), without ejecting?
推荐答案
对于 Expo IOS 目前的选择非常有限,因为 Apple 禁止获取私人设备信息.我们需要在下面创建我们自己的唯一标识符.
For Expo IOS theres currently very limited options, Since Apple forbids getting private device info. We will need to create our own unique identifier below.
我的解决方案是 uuid 和 Expo SecureStore 适用于 IOS 和 Android.
My solution is a combination of uuid and Expo SecureStore works for IOS and 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
已弃用,将在 SDK 44 中删除)
This solution will work even if app gets reinstalled, or if user switches devices and copy's all data to new device.(Expo.Constants.deviceId
is deprecated and will be removed in SDK 44)
这篇关于Expo无需弹出即可获得唯一的设备ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!