【问题标题】:How to get unique device id in flutter?如何在颤动中获得唯一的设备ID?
【发布时间】:2017-12-15 08:04:13
【问题描述】:

在 Android 中,我们有 Settings.Secure.ANDROID_ID。我不知道 iOS 等价物。 是否有一个颤振插件或一种方法可以在颤动中为 Android 和 IOS 获取唯一的设备 ID?

【问题讨论】:

  • 此唯一 ID 是否会在任何情况下更改或保持不变且永不更改?如果改变,它会改变的场景是什么?
  • 更新:因为这个帖子在 Google 上的排名非常高,所以我想在这里提一下 device_id 包已经停产。包platform_device_id 看起来它的工作方式大致相同,并且具有最近的活动。在 iOS 崩溃后,我们在日志中看到指向旧包的错误后,我们将切换到该版本。
  • 在 Android 设备上,该设备可以有多个用户。有没有办法在设备上为每个用户获取唯一 ID?

标签: flutter flutter-dependencies


【解决方案1】:

有一个名为 device_info 的插件。你可以得到它here

查看官方example here

 static Future<List<String>> getDeviceDetails() async {
    String deviceName;
    String deviceVersion;
    String identifier;
    final DeviceInfoPlugin deviceInfoPlugin = new DeviceInfoPlugin();
    try {
      if (Platform.isAndroid) {
        var build = await deviceInfoPlugin.androidInfo;
        deviceName = build.model;
        deviceVersion = build.version.toString();
        identifier = build.androidId;  //UUID for Android
      } else if (Platform.isIOS) {
        var data = await deviceInfoPlugin.iosInfo;
        deviceName = data.name;
        deviceVersion = data.systemVersion;
        identifier = data.identifierForVendor;  //UUID for iOS
      }
    } on PlatformException {
      print('Failed to get platform version');
    }

//if (!mounted) return;
return [deviceName, deviceVersion, identifier];
}

您可以将此 UUID 存储在钥匙串中。这样您就可以为您的设备设置一个唯一的 ID。

【讨论】:

  • 如何获取安卓设备ID?可以从 device_info 中检索到吗?
  • if (Platform.isAndroid) { .... identifier = build.androidId; ...} else if (Platform.isIOS) { ..... }
  • 这需要用户的任何权限吗?
  • @ScottF 你有没有想过它是否合法并且需要用户的任何许可?
  • @Chaythanya example_here 链接不起作用,我认为它已损坏。请检查并更新它。谢谢。
【解决方案2】:

空安全码

使用 Flutter 社区开发的device_info_plus 插件。这就是您可以在两个平台上获取 ID 的方式。

在您的 pubspec.yaml 文件中添加:

dependencies:
  device_info_plus: ^3.0.1

创建一个方法:

Future<String?> _getId() async {
  var deviceInfo = DeviceInfoPlugin();
  if (Platform.isIOS) { // import 'dart:io'
    var iosDeviceInfo = await deviceInfo.iosInfo;
    return iosDeviceInfo.identifierForVendor; // unique ID on iOS
  } else {
    var androidDeviceInfo = await deviceInfo.androidInfo;
    return androidDeviceInfo.androidId; // unique ID on Android
  }
}

用法:

String? deviceId = await _getId();

【讨论】:

  • 此唯一 ID 是否会在任何情况下更改或保持不变且永不更改?如果改变,它会改变的场景是什么?
  • 有时它被认为是空的,它被记录为“可以在恢复出厂设置时更改”。使用风险自负,并且可以在有根手机上轻松更改。
  • @spycbanda Google Play Store 版本对我来说具有不同的 ID 和调试开发。
  • 刚刚测试过:ID 保持不变,直到程序被删除。如果重新安装,ID就不同了。
  • 添加此插件后应用程序在 iPhone 模拟器中不起作用
【解决方案3】:

我刚刚发布了一个插件来解决您的问题。 它使用 Android 的 Settings.Secure.ANDROID_ID 并依赖 identifierForVendor 和 iOS 的钥匙串来使行为等同于 Android。 Here's the link.

【讨论】:

  • 我无法使用此插件构建我的项目,这与 SWIFT 相关...
【解决方案4】:

2021 年 1 月 3 日更新:现在推荐的方法是扩展社区插件 device_info_plus。它支持的平台比 device_info 更多,旨在支持 Flutter 支持的所有平台。这是一个示例用法:

import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:device_info_plus/device_info_plus.dart';
import 'dart:io';

Future<String> getDeviceIdentifier() async {
  String deviceIdentifier = "unknown";
  DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();

  if (Platform.isAndroid) {
    AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
    deviceIdentifier = androidInfo.androidId;
  } else if (Platform.isIOS) {
    IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
    deviceIdentifier = iosInfo.identifierForVendor;
  } else if (kIsWeb) {
    // The web doesnt have a device UID, so use a combination fingerprint as an example
    WebBrowserInfo webInfo = await deviceInfo.webBrowserInfo;
    deviceIdentifier = webInfo.vendor + webInfo.userAgent + webInfo.hardwareConcurrency.toString();
  } else if (Platform.isLinux) {
    LinuxDeviceInfo linuxInfo = await deviceInfo.linuxInfo;
    deviceIdentifier = linuxInfo.machineId;
  } 
  return deviceIdentifier;
}

【讨论】:

  • AndroidIOS 文档中所述,androidId 和 identifierForVendor 会随着时间而改变。
【解决方案5】:

使用device_id插件

  1. 在 .yaml 文件中添加以下代码。

    device_id: ^0.1.3
    
  2. 在你的类中添加导入

    import 'package:device_id/device_id.dart';
    
  3. 现在从以下位置获取设备 ID:

    String deviceid = await DeviceId.getID;
    

【讨论】:

  • 这将强制您的应用拥有android.permission.READ_PHONE_STATE。这将在将您的应用以Apps using these permissions in an APK are required to have a privacy policy set. 发布到 Play 商店时导致警告
  • 此插件已停产
【解决方案6】:

最新:

插件device_info 已给出deprecation notice 并替换为 device_info_plus

例子:

dependencies:
  device_info_plus: ^2.1.0

使用方法:

import 'package:device_info_plus/device_info_plus.dart';

DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();

AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
print('Running on ${androidInfo.model}');  // e.g. "Moto G (4)"

IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
print('Running on ${iosInfo.utsname.machine}');  // e.g. "iPod7,1"

WebBrowserInfo webBrowserInfo = await deviceInfo.webBrowserInfo;
print('Running on ${webBrowserInfo.userAgent}');  // e.g. "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0"

您可以查看here完整示例:

对于唯一 ID:

您可以使用以下代码获取唯一 ID:

if (kIsWeb) {
  WebBrowserInfo webInfo = await deviceInfo.webBrowserInfo;
  deviceIdentifier = webInfo.vendor +
      webInfo.userAgent +
      webInfo.hardwareConcurrency.toString();
} else {
  if (Platform.isAndroid) {
    AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
    deviceIdentifier = androidInfo.androidId;
  } else if (Platform.isIOS) {
    IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
    deviceIdentifier = iosInfo.identifierForVendor;
  } else if (Platform.isLinux) {
    LinuxDeviceInfo linuxInfo = await deviceInfo.linuxInfo;
    deviceIdentifier = linuxInfo.machineId;
  }
} 

【讨论】:

    【解决方案7】:

    我发布了一个新的颤振插件client_information 可能会有所帮助。它提供了一种从应用程序用户那里获取一些基本设备信息的简单方法。

    1. 添加到 pubspec.yaml
      dependencies:
        ...
        client_information: ^1.0.1
    
    1. 导入您的项目
    import 'package:client_information/client_information.dart';
    
    1. 那么你可以像这样获取设备ID
    /// Support on iOS, Android and web project
    Future<String> getDeviceId() async {
      return (await ClientInformation.fetch()).deviceId;
    }
    
    

    【讨论】:

    • 如果我重装APP,这里的androidId会变吗?
    【解决方案8】:

    在您的 .yaml 文件中添加以下代码。

    device_info_plus: ^1.0.0
    

    我使用以下方法获取支持所有平台(即)Android、IOS 和 Web 的设备信息。

    import 'dart:io';  
    import 'package:device_info_plus/device_info_plus.dart';  
    import 'package:flutter/foundation.dart' show kIsWeb;  
    
    Future<String> getDeviceIdentifier() async { 
    
        String deviceIdentifier = "unknown";  
        DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();  
    
        if (kIsWeb) {
          WebBrowserInfo webInfo = await deviceInfo.webBrowserInfo;
          deviceIdentifier = webInfo.vendor +
              webInfo.userAgent +
              webInfo.hardwareConcurrency.toString();
        } else {
          if (Platform.isAndroid) {
            AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
            deviceIdentifier = androidInfo.androidId;
          } else if (Platform.isIOS) {
            IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
            deviceIdentifier = iosInfo.identifierForVendor;
          } else if (Platform.isLinux) {
            LinuxDeviceInfo linuxInfo = await deviceInfo.linuxInfo;
            deviceIdentifier = linuxInfo.machineId;
          }
        }
        return deviceIdentifier;
    }
    

    【讨论】:

      【解决方案9】:

      如果您要投放广告,您可以使用ASIdentifierManager。您应该只将其用于广告。出于隐私原因,iOS 上的操作系统没有提供通用的 UDID 机制。

      如果您使用firebase_auth 插件,您可以匿名签名,然后使用 FirebaseUser 的 ID。这将为您提供一个特定于您的 Firebase 应用的标识符。

      【讨论】:

      • 我没有使用 firebase ,也没有投放广告。我需要一个唯一标识符来标识设备,例如 android 中的 Settings.Secure.ANDROID_ID。我想知道是否有这个插件,或者我应该为此制作一个新插件吗?
      • 我认为您所要求的在 iOS 上是不可能的。不过,您可以通过将 guid 写入文件来轻松识别应用安装。
      • 当我们卸载并重新安装移动应用程序时,Firebase 令牌已自动更改,因此令牌的唯一性不起作用。我们不能用它来识别同一个用户。
      • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
      猜你喜欢
      • 1970-01-01
      • 2019-09-21
      • 2013-11-05
      • 1970-01-01
      • 2017-01-08
      • 2014-11-13
      • 2016-05-12
      • 1970-01-01
      • 2020-06-15
      相关资源
      最近更新 更多