首页 > 解决方案 > Cordova 设备 UUID 是否与电容器设备 UUID 相同?

问题描述

我们正在从 Ionic Cordova 应用程序迁移到 Ionic Capacitor 应用程序。虽然我知道 Capacitor 仍然支持 Cordova 插件,但我们尝试尽可能多地迁移。

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

这是我的比较:

iOS 科尔多瓦:

[[device identifierForVendor] UUIDString]

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

安卓科尔多瓦:

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/0bcd833a6503061be45253b21fca9bd75576​​efc8/device/ios/Plugin/DevicePlugin.swift#L28

安卓电容:

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

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

标签: cordovacordova-pluginscapacitorcapacitor-plugin

解决方案


iOS

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

安卓

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

科尔多瓦插件

根据 Cordova 插件指南

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

电容插件

如果您浏览 Capacitor 插件的代码,则作为主入口点的文件调用getContext()并直接将其传递给Device() 构造函数

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


推荐阅读