IOSPlatformBridge.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { native } from 'cc';
  2. import { PlatformBridge } from './PlatformBridge';
  3. import { PlatformType } from './PlatformBridge';
  4. export class IOSPlatformBridge extends PlatformBridge {
  5. readonly type = PlatformType.IOS;
  6. private _deviceID: string | null = null;
  7. getUrlParam(_name: string): string | null {
  8. console.error('[IOS] 获取深度链接参数能力未实现');
  9. return null;
  10. }
  11. /** IDFV:同一开发商范围内的设备标识,不保证卸载重装后保持不变。 */
  12. getDeviceID(): string | null {
  13. if (this._deviceID) return this._deviceID;
  14. try {
  15. // Creator 3.8 的声明只有 Android 签名;iOS 无参 selector 只传类名和方法名。
  16. const iosReflection = native.reflection as unknown as {
  17. callStaticMethod(className: string, methodName: string): unknown;
  18. };
  19. const deviceID = iosReflection.callStaticMethod(
  20. 'CocosIOSPlatformBridge',
  21. 'getDeviceID',
  22. );
  23. if (typeof deviceID === 'string' && deviceID.trim().length > 0) {
  24. this._deviceID = deviceID.trim();
  25. return this._deviceID;
  26. }
  27. } catch (error) {
  28. console.error('[IOS] 获取 IDFV 失败:', error);
  29. }
  30. // IDFV 在设备重启后首次解锁前可能不可用;不缓存失败,下次调用重试。
  31. return null;
  32. }
  33. }