How to get unique device id in flutter?

丶灬走出姿态 提交于 2019-12-08 14:47:19

问题


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 and IOS in flutter?


回答1:


There is a plugin called device_info. You can get it here. In iOS there is no way you can get a unique device ID. There is a wok around for this.

 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.




回答2:


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.




回答3:


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.




回答4:


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 {
  DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
  if (Theme.of(context).platform == TargetPlatform.iOS) {
    IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo;
    return iosDeviceInfo.identifierForVendor; // unique ID on iOS
  } else {
    AndroidDeviceInfo androidDeviceInfo = await deviceInfo.androidInfo;
    return androidDeviceInfo.androidId; // unique ID on Android
  }
}

Use it like:

String deviceId = await _getId();

Source




回答5:


add in your following code in your .yaml file.

device_id: ^0.1.3

add import in your class

import 'package:device_id/device_id.dart';

now get device id from:

String deviceid = await DeviceId.getID;


来源:https://stackoverflow.com/questions/45031499/how-to-get-unique-device-id-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!