IOSPlatformBridge.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. /** 请求 ATT 系统授权;异步展示,不阻塞初始化,也不强制要求用户同意。 */
  8. requestTrackingAuthorization(): void {
  9. try {
  10. const iosReflection = native.reflection as unknown as {
  11. callStaticMethod(className: string, methodName: string): unknown;
  12. };
  13. iosReflection.callStaticMethod('CocosIOSPlatformBridge', 'requestTrackingAuthorization');
  14. } catch (error) {
  15. console.error('[IOS] 请求 ATT 授权失败:', error);
  16. }
  17. }
  18. getUrlParam(_name: string): string | null {
  19. console.error('[IOS] 获取深度链接参数能力未实现');
  20. return null;
  21. }
  22. /** 首次生成 UUID 并存入本机 Keychain,后续读取;不是硬件编号。 */
  23. getDeviceID(): string | null {
  24. if (this._deviceID) return this._deviceID;
  25. try {
  26. // Creator 3.8 的声明只有 Android 签名;iOS 无参 selector 只传类名和方法名。
  27. const iosReflection = native.reflection as unknown as {
  28. callStaticMethod(className: string, methodName: string): unknown;
  29. };
  30. const deviceID = iosReflection.callStaticMethod(
  31. 'CocosIOSPlatformBridge',
  32. 'getDeviceID',
  33. );
  34. if (typeof deviceID === 'string' && deviceID.trim().length > 0) {
  35. this._deviceID = deviceID.trim();
  36. return this._deviceID;
  37. }
  38. } catch (error) {
  39. console.error('[IOS] 获取 Keychain 设备码失败:', error);
  40. }
  41. // Keychain 暂不可用或写入失败时不缓存结果,下次调用重试。
  42. return null;
  43. }
  44. }