中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
如何向用户请求授权广告跟踪权限
微信扫码分享
import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { common, abilityAccessCtrl } from '@kit.AbilityKit'; requestOAIDTrackingConsentPermissions(context: common.Context): void { // 进入页面时触发动态授权弹框,向用户请求授权广告跟踪权限 const atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); try { atManager.requestPermissionsFromUser(context, ["ohos.permission.APP_TRACKING_CONSENT"]).then((data) => { if (data.authResults[0] == 0) { hilog.info(0x0000, 'testTag', '%{public}s', 'request permission succeed'); } else { hilog.info(0x0000, 'testTag', '%{public}s', 'user rejected'); } }).catch((err: BusinessError) => { hilog.error(0x0000, 'testTag', '%{public}s', `request permission failed, error: ${err.code} ${err.message}`); }) } catch (err) { hilog.error(0x0000, 'testTag', '%{public}s', `catch err->${err.code}, ${err.message}`); } }