【问题标题】:Expo get unique device id without ejectingExpo在不弹出的情况下获得唯一的设备ID
【发布时间】:2017-10-21 13:18:28
【问题描述】:

This library 可以让你获取Android设备的唯一设备ID/Mac地址,重装后不会改变。

Expo.Constants.deviceId 在每次重新安装后都会发生变化(即使应用版本号相同)。

有没有办法在不弹出的情况下获得一个在重新安装后不会更改的 Android 唯一 ID(至少对于相同版本)?

【问题讨论】:

标签: android react-native expo


【解决方案1】:

对于 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)

即使重新安装应用程序,或者如果用户切换设备并将所有数据复制到新设备,此解决方案也将起作用。 (Expo.Constants.deviceId 已弃用,将在 SDK 44 中删除)

【讨论】:

  • 我猜 'secure_deviceid' 在应用更新后保留,但在安装之间没有?
  • 即使重新安装也会保留它,因为SecureStore 存储在系统钥匙串中,并且根据 Expo 文档将保留在“使用 expo-secure-store 存储的数据可以在应用程序安装中持续存在”。 docs.expo.io/versions/latest/sdk/securestore
  • 在 Android 上的应用安装之间是否也会持续存在? Doc 说:请注意,对于 iOS 独立应用程序,使用 expo-secure-store 存储的数据可以在应用程序安装中持续存在。但没有提到 Android
  • @yemd SecureStore for Android 存储在SharedPreferences 这可能会有所帮助stackoverflow.com/questions/24650870/…
  • 它每次为同一设备提供不同的值我需要为一个设备实现相同的唯一 ID
【解决方案2】:

使用来自 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;
  }
}

【讨论】:

  • 但是 ios 呢?它说它将在 ios 上返回 null。我需要在两个平台上都可以使用的东西
  • @AbhishekAnand 我添加了一个例子
【解决方案3】:

猜猜您可以为此目的使用 facebook 模块。 https://docs.expo.io/versions/latest/sdk/facebook-ads/#currentdevicehash

不确定引擎盖下会发生什么 - 但看起来它在应用重新安装、设备重启等之间是独一无二的。

【讨论】:

  • 请将所有相关信息添加到您的答案中,而不是链接到外部资源
  • @NicoHaase 不知道为什么有人看不懂这个世博会官方文档。
【解决方案4】:

只需使用 react-native-device-info 中的 getuniqueid() 方法。适用于 iOS 和 android 以唯一标识设备。

【讨论】:

  • 这在 Expo 管理的工作流程中不起作用
猜你喜欢
  • 2020-07-12
  • 1970-01-01
  • 2013-11-05
  • 2017-12-15
  • 2016-05-12
  • 2018-06-25
  • 2019-08-28
  • 1970-01-01
  • 2015-02-25
相关资源
最近更新 更多