作者:狼哥 团队:坚果派 团队介绍:坚果派由坚果创建,团队拥有12个华为HDE,以及若干其他领域的三十余位万粉博主运营。专注于分享的技术包括HarmonyOS/OpenHarmony,ArkUI-X,元服务,服务卡片,华为自研语言,团队成员聚集在北京,上海,南京,深圳,广州,宁夏等地,欢迎合作。
DevEco Studio:4.0Release
OpenHarmony SDK API10
开发板:润和DAYU200
隐私协议对话框静态共享库模块是使用Static Library静态共享库模块实现隐私协议对话框和隐私协议显示,对话框使用自定义对话框实现,隐私协议显示在一个Webview组件页面上,支持本地html文件和http或https返回html文件。
本库由 狼哥[1],完成迁移。
ohpm install @nutpi/privacy_dialog
OpenHarmony ohpm 环境配置等更多内容,请参考如何安装 OpenHarmony ohpm 包[2]
1.在EntryAbility.ts文件里创建首选项数据库
import { CustomDialogPrivacy,PreferencesUtil } from '@nutpi/privacy_dialog'
let preferencesUtil = new PreferencesUtil();
onCreate(want, launchParam) {
// 创建首选项数据库
preferencesUtil.createPrivacyPreferences(this.context);
// 设置隐私协议默认不同意
preferencesUtil.saveDefaultPrivacy(false);
}
2.在Index.ets页面,调用对话框
import { CustomDialogPrivacy,PreferencesUtil } from '@nutpi/privacy_dialog'
let preferencesUtil = new PreferencesUtil();
import('privacy_dialog/src/main/ets/components/PrivacyPage'); // 引入静态共享包中的命名路由页面
// 开始显示隐私协议对话框
/**
* 如果localHtml参数为true,urlPage参数为空,显示默认隐私协议
* 如果localHtml参数为true,urlPage参数不为空,显示urlPage参数本地html文件
* 如果localHtml参数为false,urlPage参数为空,显示默认隐私协议
* 如果localHtml参数为false,urlPage参数不为空,显示urlPage参数http或https返回html文件
*/
// privacyPage为三方库里的routeName
private privacyPage: string = 'privacyPage'
// RouterParams参数1为localHtml=true时,使用本地rawfile目录下html文件,为false时,使用远程http隐私协议
// RouterParams参数2根据参数1而定,为true时,写本地html文件名如:privacy.html,为false时,如:http://xxxx/zh-cn_userAgreement.html
openPrivacyPage() {
try {
router.pushNamedRoute({
name: this.privacyPage,
params: new RouterParams(true, 'privacy.html')
}).then(() => {
console.info("xx push page success");
}).catch((err: BusinessError) => {
console.error(`xx pushUrl failed, code: ${err.code}, msg: ${err.message}`);
})
} catch (err) {
let message = (err as BusinessError).message
let code = (err as BusinessError).code
console.error(`xx pushNamedRoute failed, code is ${code}, message is ${message}`);
}
}
privacyDialogController: CustomDialogController = new CustomDialogController({
builder: CustomDialogPrivacy({
openPrivacy: this.openPrivacyPage
}),
autoCancel: false,
alignment: DialogAlignment.Center,
customStyle: true
})
onPageShow() {
console.info('xx onPageShow 显示隐私协议')
preferencesUtil.getChangePrivacy().then((value) => {
console.info(`xx onPageShow 获取隐私协议状态:${value}`)
if (!value) {
this.privacyDialogController.open()
}
})
}
onPageHide() {
console.info(`xx Index -> onPageHide Close Start`)
this.privacyDialogController.close()
console.info(`xx Index -> onPageHide Close End`)
}
aboutToDisappear() {
console.info(`xx Index -> aboutToDisappear`)
this.privacyDialogController.close()
}
// 结束显示隐私协议对话框
创建对话框时,可选参数以下:
// 隐私协议标题
private title?: string = '协议和隐私政策提示'
// 前辍隐私协议信息
private prefixMessage?: string = '感谢您选择xxx元服务!我们非常重视您的个人信息和隐私保护。为更好地保障您的个人权益,在使用产品前,请您认真阅读'
// 后辍隐私协议信息
private suffixMessage?: string = '的全部内容。'
// 隐私协议信息,点击可以跳转
private privacyMessage?: string = '《协议与隐私政策》'
3.点击设备桌面图标运行查看效果
本项目基于 Apache[3] ,请自由地享受和参与开源。欢迎大家与坚果派建立联系。
参考资料
[1]
狼哥: https://blog.csdn.net/qq_39132095
[2]
如何安装 OpenHarmony ohpm 包: https://gitee.com/openharmony-tpc/docs/blob/master/OpenHarmony_har_usage.md
[3]
Apache: https://gitee.com/nutpi/relationship/blob/master/library/LICENSE