HarmonyOS 申请提供权限启动页,权限使用说明相关demo

应用中有使用隐私权限的设置, 需要参考相关demo, 希望提供一下相关的文档或资料, 谢谢

HarmonyOS
2024-12-23 15:51:47
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

1、组件集成

在对应模块中的oh-package.json5中添加依赖,如:entry/oh-package.json5

"dependencies": {
  "@hms-paf/ui-widget-base": "14.0.1-103",
  "@hms-paf/ui-widget-consent": "14.0.1-103",
  "@hms-paf/ui-widget-permission": "14.0.1-103",
  "@hms-security/consent": "6.12.0-313",
  "@network/grs": "^7.0.6-300"
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

2、引入包

在项目的入口文件中,进行初始化,如:EntryAbility.ets

// 导入Paf UiWidget包
import { PafUiWidget } from '@hms-paf/ui-widget-base';
// 导入GRS包,用于初始化GRS
import { GrsFileUtil } from '@network/grs';
// 导入consent相关包
import { AgreementCallBack, PafAgreement, PafClientInfo } from '@hms-paf/ui-widget-consent';
// 业务自己适配的接口
import { AgreementConfig } from '../Component/AgreementConfig';
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

3、初始化

在项目的入口文件中,进行初始化,如:EntryAbility.ets

async onWindowStageCreate(windowStage: window.WindowStage) {
  // Paf UiWidget 初始化
  PafUiWidget.init(windowStage)
  // Grs初始化
  GrsFileUtil.copyGrsConfigFile(this.context, false);

  // 业务自己适配的接口初始化paf
  AgreementConfig.context = this.context;
  AgreementConfig.windowStage = windowStage;
  let agreementCallBack: AgreementCallBack = {
    loadWelcomePage: async () => {
      windowStage.loadContent('pages/permissionNormal');
    },
    loadMainPage: async () => {
      windowStage.loadContent('pages/index');
    },
    signCallBack: new AgreementConfig.SignCallBack(),
    revokeCallBack: new AgreementConfig.RevokeCallBack(),
    queryRecordsCallBack: new AgreementConfig.QueryCallBack(),
  }
  let clientInfoIn: PafClientInfo = await AgreementConfig.clientInfo();
  let popupPermission: boolean = false;
  // 开关,控制协议签署方式(目前switchToNew 属性测试代码,先用init,initAsync等正式版本后再使用)
  let switchToNew: boolean = AppStorage.get<boolean>('Paf.UseNewConsentWay') || false;
  if (switchToNew) {
    popupPermission = await PafAgreement.initAsync(this.context, clientInfoIn, AgreementConfig.userAccount(),
      AgreementConfig.getAgreementTypes(), agreementCallBack);
  } else {
    // clientInfoIn仅在初始化使用一次
    popupPermission = PafAgreement.init(this.context, clientInfoIn, AgreementConfig.userAccount(),
      AgreementConfig.getAgreementTypes(), agreementCallBack);
  }
  if (popupPermission) {
    // 拉起权限启动页
  } else {
    // 应用业务首页
  }
}
// 监听语言变化
onConfigurationUpdate(newConfig: Configuration) {
  PafUiWidget.configurationUpdate(newConfig)
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.

4、权限启动页示例 - 字符串拼接

import I18n from '@ohos.i18n';
import router from '@ohos.router';
import { PafSpan, PafText } from '@hms-paf/ui-widget-base';
import {
  PafPermission,
  PafPermissionItem,
  PafPopPermissionCancel,
  PafPopPermissionDesc
} from '@hms-paf/ui-widget-permission';

class DeclareDemo {
  format: string | Resource = ''
  args: PafSpan[] = []
}

@Entry
@Component
struct Permission {
  @State basicTitle: string = ''
  @State fullTitle: string = ''
  @State declares: Array<PafText> = []
  @State dialogDeclare: Array<PafText> = []
  declareDemo1: DeclareDemo = {
    format: `本应用需%1$s,调用%2$s、%3$s权限,获取XX信息,以为您提供XX服务。我们仅在您使用具体功能业务时,才会触发上述行为收集使用相关的个人信息。详情请参阅%4$s、%5$s。`,
    args: [
      new PafSpan($r('app.string.paf_text_arg1')).emphasis(),
      new PafSpan('XX').emphasis(),
      new PafSpan('XX').emphasis(),
      new PafSpan($r('app.string.paf_text_arg4')).hyperlink(() => {
        router.pushUrl({ url: 'pages/privacyStatement' });
      }),
      new PafSpan('权限使用说明').hyperlink(() => {
        this.permissionDescDlg.open()
      })
    ]
  }
  declareDemo2: DeclareDemo = {
    format: `请您仔细阅读上述声明,点击“同意”,即表示您知悉并同意我们向您提供本应用服务。`,
    args: []
  }
  dialogDeclareDemo1: DeclareDemo = {
    format: `全量模式下,本应用需%1$s,调用%2$s、%3$s权限,获取XX信息,以为您提供XX服务。我们仅在您使用具体功能业务时,才会触发上述行为收集使用相关的个人信息。详情请参阅%4$s。`,
    args: [
      new PafSpan($r('app.string.paf_text_arg1')).emphasis(),
      new PafSpan('XX').emphasis(),
      new PafSpan('XX').emphasis(),
      new PafSpan($r('app.string.paf_text_arg4')).hyperlink(() => {
        router.pushUrl({ url: 'pages/privacyStatement' });
      })
    ]
  }
  dialogDeclareDemo2: DeclareDemo = {
    format: `请您仔细阅读上述声明,点击“同意基本(全量)模式”,即表示您知悉并同意我们向您提供本应用服务。`,
    args: []
  }
  permissionDescDlg: CustomDialogController = new CustomDialogController({
    builder: PafPopPermissionDesc({
      title: $r('app.string.paf_permission_desc_title'),
      desc: $r('app.string.paf_permission_desc_desc'),
      items: [new PafPermissionItem("权限名称", "包括xx(子权限),用于XXXX"),
        new PafPermissionItem("权限名称2", "用于XXXX"),
        new PafPermissionItem("权限名称3", "用于XXXX"),
        new PafPermissionItem("权限名称4", "用于XXXX"),
      ]
    }),
    alignment: DialogAlignment.Bottom,
    autoCancel: true,
    customStyle: true
  })
  permissionCancelDlg: CustomDialogController = new CustomDialogController({
    builder: PafPopPermissionCancel({
      basicTitle: this.basicTitle,
      fullTitle: this.fullTitle,
      fullDesc: this.dialogDeclare,
      fullService: () => {
        console.log(`PafAgreement popPermissionCancel fullService`);
        router.replaceUrl({ url: 'pages/index' });
      }
    }),
    alignment: DialogAlignment.Bottom,
    autoCancel: true,
    customStyle: true
  })

  aboutToAppear() {
    this.basicTitle = "您可以选择“同意基本模式”以使用华为XX基本服务(播放本地歌曲),该服务需联网,调用存储权限。"
    this.fullTitle = "您可以选择“同意全量模式”以使用更多功能(如音乐推荐、歌单等)。"
    this.declares = [
      new PafText().format(this.declareDemo1.format, this.declareDemo1.args),
      new PafText().format(this.declareDemo2.format, this.declareDemo2.args)
    ]
    this.dialogDeclare = [
      new PafText().format(this.dialogDeclareDemo1.format, this.dialogDeclareDemo1.args),
      new PafText().format(this.dialogDeclareDemo2.format, this.dialogDeclareDemo2.args)
    ]
  }

  cancel() {
    this.permissionCancelDlg.open()
  }

  confirm() {
    router.replaceUrl({ url: 'pages/index' });
  }

  @Builder
  payloadBuilder() {
    PafPermission({
      appImg: $r('app.media.icon'),
      appName: $r('app.string.app_name'),
      appDesc: "声明文本使用PafText().format,支持字符串和资源字符串",
      permissionDeclare: $declares,
      confirm: () => {
        this.confirm()
      },
      cancel: () => {
        this.cancel()
      },
      //仅处理影响账号登陆的准备工作
      beforeSign:() => {

      }
    })
  }

  build() {
    Row() {
      this.payloadBuilder()
    }
    .height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.

5、权限使用说明弹窗示例

/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

import { PafPermissionItem, PafPopPermissionDesc } from '@hms-paf/ui-widget-permission';

@Entry
@Component
struct PermissionDescDlg {
  //创建弹出框,引入PafPopPermissionDesc组件
  permissionDescDlg: CustomDialogController = new CustomDialogController({
    builder: PafPopPermissionDesc({
      title: $r('app.string.paf_permission_desc_title'),
      desc: $r('app.string.paf_permission_desc_desc'),
      confirm: () => {
      },
      customColor: Color.Blue,
      items: [new PafPermissionItem("权限名称", "包括xx(子权限),用于XXXX"),
        new PafPermissionItem("权限名称2", "用于XXXX"),
        new PafPermissionItem("权限名称3", "用于XXXX"),
        new PafPermissionItem("权限名称4", "用于XXXX")
      ]
    }),
    alignment: DialogAlignment.Bottom,
    autoCancel: true,
    customStyle: true
  })

  build() {
    Row() {
    }
    .height('100%')
    .width('100%')
    .onAppear(() => {
      this.permissionDescDlg.open()
    })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
分享
微博
QQ
微信
回复
2024-12-23 18:53:06


相关问题
HarmonyOS 系统权限申请demo——拍照
713浏览 • 1回复 待解决
HarmonyOS 请提供AVRecorder demo示例
886浏览 • 1回复 待解决
HarmonyOS 请提供个路由跳转Demo
1205浏览 • 1回复 待解决
HarmonyOS 动态申请权限申请不了
833浏览 • 1回复 待解决
HarmonyOS 位置权限申请
951浏览 • 1回复 待解决
HarmonyOS 如何实现下列功能,请提供demo
1512浏览 • 1回复 待解决
请提供HarmonyOS硬编硬解demo
1305浏览 • 1回复 待解决
HarmonyOS权限申请问题
1198浏览 • 1回复 待解决
HarmonyOS 申请使用受限开放权限
1090浏览 • 1回复 待解决
HarmonyOS 权限申请拒绝问题
954浏览 • 1回复 待解决
HarmonyOS 请提供自定义组件封装demo
1346浏览 • 2回复 待解决
HarmonyOS 如何跳转权限设置
638浏览 • 1回复 待解决