Cordova 设备 UUID 与电容器设备 UUID 相同吗? Cordova 插件电容器插件

问题描述

我们正在从 Ionic Cordova 应用迁移到 Ionic Capacitor 应用。虽然我知道 Capacitor 仍然支持 Cordova 插件,但我们会尽可能地进行迁移。

我们的应用依赖于 Cordova 设备插件提供的设备 UUID。这和Capacitor提供的设备UUID一样吗?我尝试比较两个存储库,但我不完全确定 Android 是否实际上相同(下面给出的方法中的第一个参数是什么)?

这是我的比较:

iOS Cordova:

[[device identifierForvendor] UUIDString]

来源:https://github.com/apache/cordova-plugin-device/blob/2fc1d3cac0ed37a37de6a6aa18e7955342037a1d/src/ios/CDVDevice.m#L52-L74

Android Cordova:

Settings.Secure.getString(this.cordova.getActivity().getContentResolver(),android.provider.Settings.Secure.ANDROID_ID);

来源:https://github.com/apache/cordova-plugin-device/blob/2fc1d3cac0ed37a37de6a6aa18e7955342037a1d/src/android/Device.java#L111-L114

iOS 电容器:

UIDevice.current.identifierForvendor!.uuidString

来源:https://github.com/ionic-team/capacitor-plugins/blob/0bcd833a6503061be45253b21fca9bd75576efc8/device/ios/Plugin/DevicePlugin.swift#L28

Android 电容器:

Settings.Secure.getString(this.context.getContentResolver(),android.provider.Settings.Secure.ANDROID_ID);

来源:https://github.com/ionic-team/capacitor-plugins/blob/0bcd833a6503061be45253b21fca9bd75576efc8/device/android/src/main/java/com/capacitorjs/plugins/device/Device.java#L43-L45

解决方法

iOS

是一样的。在 iOS 的情况下,唯一的区别是 uuidString 的访问方式。 Cordova 插件是用 Objective-C 编写的,而 Capacitor 插件是用 Swift 编写的。不过应该为两者返回相同的值。

安卓

Android 插件似乎也引用了相同的值,只是方式不同。两者都将应用活动上下文中的内容解析器作为第一个参数传递给 Settings.Secure.getString()this.context.getContentResolver()this.cordova.getActivity().getContentResolver() 之间的区别在于访问 Context 的方式(Android 开发中的一个大 topic)。

Cordova 插件

来自 Cordova 插件 guidelines

getContext() 和 getActivity() 可以返回需要的 Context 对象

电容器插件

如果你通过Capacitor插件的代码,作为主入口点的file调用getContext()并直接将其传递给Device() constructor

简而言之,两者都指的是同一个活动上下文。 this.contextthis.cordova.getActivity()

相同