问题描述
我们正在从 Ionic Cordova 应用迁移到 Ionic Capacitor 应用。虽然我知道 Capacitor 仍然支持 Cordova 插件,但我们会尽可能地进行迁移。
我们的应用依赖于 Cordova 设备插件提供的设备 UUID。这和Capacitor提供的设备UUID一样吗?我尝试比较两个存储库,但我不完全确定 Android 是否实际上相同(下面给出的方法中的第一个参数是什么)?
这是我的比较:
iOS Cordova:
[[device identifierForvendor] UUIDString]
Android Cordova:
Settings.Secure.getString(this.cordova.getActivity().getContentResolver(),android.provider.Settings.Secure.ANDROID_ID);
iOS 电容器:
UIDevice.current.identifierForvendor!.uuidString
Android 电容器:
Settings.Secure.getString(this.context.getContentResolver(),android.provider.Settings.Secure.ANDROID_ID);
解决方法
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.context
与 this.cordova.getActivity()