How to get unique device id in flutter?

前端 未结 6 1636
没有蜡笔的小新
没有蜡笔的小新 2020-12-03 16:28

In Android we have, Settings.Secure.ANDROID_ID. I do not know the iOS equivalent. Is there a flutter plugin or a way to get a unique device id for both Android

相关标签:
6条回答
  • 2020-12-03 17:02

    There is a plugin called device_info. You can get it here. Check the official 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];
    }
    

    You can store this UUID in the Keychain. This way you can set an unique ID for your device.

    0 讨论(0)
  • 2020-12-03 17:04

    Use device_id plugin

    1. Add in your following code in your .yaml file.

      device_id: ^0.1.3
      
    2. Add import in your class

      import 'package:device_id/device_id.dart';
      
    3. Now get device id from:

      String deviceid = await DeviceId.getID;
      
    0 讨论(0)
  • 2020-12-03 17:05

    If you're serving ads you can use ASIdentifierManager. You should only use it for ads. There is no general UDID mechanism provided by the OS on iOS, for privacy reasons.

    If you're using firebase_auth plugin you could signInAnonymously and then use the id of the FirebaseUser. This will give you an identifier that is specific to your Firebase app.

    0 讨论(0)
  • 2020-12-03 17:12

    UPDATE: Because this thread has a very high Google ranking I wanted to mention here that the device_id package has been discontinued. The package platform_device_id looks like it works about the same way though, and has very recent activity. We're switching over to that after seeing errors in the log after iOS crashes which point to the old package.

    0 讨论(0)
  • 2020-12-03 17:14

    Use device_info plugin developed by Flutter team itself. This is how you can get IDs on both platform.

    In your pubspec.yaml file add this:

    dependencies:
      device_info: ^0.4.0+4
    

    Create a method:

    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
      }
    }
    

    Usage:

    String deviceId = await _getId();
    

    Source

    0 讨论(0)
  • 2020-12-03 17:20

    I just published a plugin to provide a solution to your problem. It uses Settings.Secure.ANDROID_ID for Android and relies on identifierForVendor and the keychain for iOS to make the behaviour equivalent to Android's. Here's the link.

    0 讨论(0)
提交回复
热议问题