| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- import { native } from 'cc';
- import { PlatformBridge } from './PlatformBridge';
- import { PlatformType } from './PlatformBridge';
- export class IOSPlatformBridge extends PlatformBridge {
- readonly type = PlatformType.IOS;
- private _deviceID: string | null = null;
- /** 请求 ATT 系统授权;异步展示,不阻塞初始化,也不强制要求用户同意。 */
- requestTrackingAuthorization(): void {
- try {
- const iosReflection = native.reflection as unknown as {
- callStaticMethod(className: string, methodName: string): unknown;
- };
- iosReflection.callStaticMethod('CocosIOSPlatformBridge', 'requestTrackingAuthorization');
- } catch (error) {
- console.error('[IOS] 请求 ATT 授权失败:', error);
- }
- }
- getUrlParam(_name: string): string | null {
- console.error('[IOS] 获取深度链接参数能力未实现');
- return null;
- }
- /** 首次生成 UUID 并存入本机 Keychain,后续读取;不是硬件编号。 */
- getDeviceID(): string | null {
- if (this._deviceID) return this._deviceID;
- try {
- // Creator 3.8 的声明只有 Android 签名;iOS 无参 selector 只传类名和方法名。
- const iosReflection = native.reflection as unknown as {
- callStaticMethod(className: string, methodName: string): unknown;
- };
- const deviceID = iosReflection.callStaticMethod(
- 'CocosIOSPlatformBridge',
- 'getDeviceID',
- );
- if (typeof deviceID === 'string' && deviceID.trim().length > 0) {
- this._deviceID = deviceID.trim();
- return this._deviceID;
- }
- } catch (error) {
- console.error('[IOS] 获取 Keychain 设备码失败:', error);
- }
- // Keychain 暂不可用或写入失败时不缓存结果,下次调用重试。
- return null;
- }
- }
|