
简体中文
因为uni-app下开发者的js代码执行较晚,所以框架提供了原生隐私协议框,但自定义性较差。
uni-app x并不需要这套机制,开发者的代码就是原生代码,执行时机很早,可以自己弹出隐私协议政策。(如在app launch生命周期中弹出dialogPage的协议框)
但应用开发者和插件开发者,有时需要共享隐私协议是否同意的状态。所以提供了如下一批能力。
uni.getPrivacySetting
:获取用户是否同意了隐私协议uni.resetPrivacyAuthorization
:重置隐私协议状态。适用于隐私协议变更,需要重新同意的场景uni.onPrivacyAuthorizationChange
、uni.offPrivacyAuthorizationChange
:监听和取消监听用户是否同意隐私协议manifest.json
中, app
节点下initPrivacyAuthorization
为 auto
时,安卓、鸿蒙平台隐私状态初始值为 disagree
,iOS平台隐私状态初始值为 agree
获取隐私协议状态
Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS |
---|---|---|---|---|---|
x | 4.41 | 4.31 | 4.31 | 4.31 | 4.61 |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options | GetPrivacySettingOptions | 是 | - | ||||||||||||||||||||||||||
|
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
needAuthorization | boolean | 是 | - | 是否需要用户授权隐私协议(用户之前同意过返回false,没同意过则返回true) | |
privacyContractName | string | 否 | - | 隐私授权协议的名称 |
重置隐私协议状态为未同意
适用于隐私协议变更,需要重新同意的场景。
Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS |
---|---|---|---|---|---|
x | - | 4.31 | 4.31 | 4.31 | 4.61 |
开启监听隐私协议状态改变
Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS |
---|---|---|---|---|---|
x | - | 4.31 | 4.31 | 4.31 | 4.61 |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
callback | (res: PrivacyChangeResult) => void | 是 | - |
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
needAuthorization | boolean | 是 | - | 是否需要用户授权隐私协议(用户之前同意过返回false,没同意过则返回true) |
类型 |
---|
number |
取消监听隐私协议状态改变
Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS |
---|---|---|---|---|---|
x | - | 4.31 | 4.31 | 4.31 | 4.61 |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
id | number | 是 | - | 开启监听隐私协议状态改变返回的id |
示例为 alpha 分支:hello uni-app x(alpha)
master 分支:hello uni-app x(master)
该 API 不支持 Web,请运行 hello uni-app x 到 App 平台体验
<template>
<view class="uni-padding-wrap">
<page-head :title="title"></page-head>
<view class="item-box">
<text>当前应用隐私授权状态:</text>
<text>{{ appPrivacy }}</text>
</view>
<!-- #ifdef MP-WEIXIN -->
<view class="item-box">
<text>隐私授权协议的名称:</text>
<text>{{ privacyContractName }}</text>
</view>
<!-- #endif -->
<view>
<button class="privacy-button" type="primary" @tap="getPrivacySetting">
获取隐私协议授权状态
</button>
<button class="privacy-button" type="primary" open-type="agreePrivacyAuthorization">
同意隐私协议专用按钮
</button>
<!-- #ifdef APP -->
<button class="privacy-button" type="primary" @tap="resetPrivacyAuthorization">
重置隐私协议授权状态
</button>
<button class="privacy-button" @tap="openPrivacyDialog">
显示隐私政策弹框
</button>
<!-- #endif -->
</view>
</view>
</template>
<script>
export default {
data() {
return {
title: '隐私信息授权',
appPrivacy: '未获取',
privacyContractName: "",
listenId: 0
}
},
onReady() {
// #ifdef APP
//添加 隐私协议监听
const id = uni.onPrivacyAuthorizationChange((res) => {
this.appPrivacy = res.needAuthorization ? "未同意" : "已同意"
const privacyState = "监听到隐私协议状态已变更为 " + this.appPrivacy;
uni.showToast({
"position": "bottom",
"title": privacyState
})
})
this.listenId = id;
uni.showToast({
"position": "bottom",
"title": "开启监听隐私协议状态"
})
// #endif
},
onUnload() {
// #ifdef APP
//注销监听
uni.offPrivacyAuthorizationChange(this.listenId)
this.listenId = 0;
uni.showToast({
"position": "bottom",
"title": "已停止监听隐私协议状态"
})
// #endif
},
methods: {
getPrivacySetting() {
uni.getPrivacySetting({
success: (res) => {
this.appPrivacy = res.needAuthorization ? "未同意" : "已同意"
// #ifdef MP-WEIXIN
this.privacyContractName = res.privacyContractName
// #endif
}
})
},
resetPrivacyAuthorization(){
uni.resetPrivacyAuthorization()
},
openPrivacyDialog(){
uni.openDialogPage({
url: '/pages/component/button/privacy',
})
}
}
}
</script>
<style>
.item-box {
margin-bottom: 10px;
display: flex;
flex-direction: row;
justify-content: space-between;
}
.privacy-button{
margin-top: 5px;
margin-bottom: 5px;
}
</style>
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
errMsg | string | 是 | - | 错误信息 |