隐私API
因为uni-app下开发者的js代码执行较晚,所以框架提供了原生隐私协议框,但自定义性较差。
uni-app x并不需要这套机制,开发者的代码就是原生代码,执行时机很早,可以自己弹出隐私协议政策。(如在app launch生命周期中弹出dialogPage的协议框)
但应用开发者和插件开发者,需要监听和共享隐私协议是否同意。所以提供了如下一批能力。
uni.getPrivacySetting:获取用户是否同意了隐私协议 uni.resetPrivacyAuthorization:重置隐私协议状态。适用于隐私协议变更,需要重新同意的场景 uni.onPrivacyAuthorizationChange、uni.offPrivacyAuthorizationChange:监听和取消监听用户是否同意隐私协议 - 在 button组件中,提供了属性open-type="agreePrivacyAuthorization"。之所以同意隐私协议是按钮,而不是API,是因为需要用户真实的同意。避免插件作者通过API非正常设置隐私协议为同意。所以开发者务必在隐私协议的同意按钮处使用 open-type="agreePrivacyAuthorization" 的 button组件。
manifest.json中, app 节点下initPrivacyAuthorization 为 auto 时,安卓、鸿蒙平台隐私状态初始值为 disagree,iOS平台隐私状态初始值为 agree
uni.getPrivacySetting(options)
获取隐私协议状态
getPrivacySetting 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS |
| x | 4.41 | 4.31 | 4.31 | 4.31 | 4.61 |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| options | GetPrivacySettingOptions | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 | | success | (result: GetPrivacySettingSuccessResult) => void | 否 | null | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
| | x | 4.41 | 4.31 | 4.31 | 4.31 | 4.61 |
| 接口调用成功的回调函数 | | fail | (result: any) => void | 否 | null | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
| | x | 4.41 | 4.31 | 4.31 | 4.31 | 4.61 |
| 接口调用失败的回调函数 | | complete | (result: any) => void | 否 | null | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
| | x | 4.41 | 4.31 | 4.31 | 4.31 | 4.61 |
| 接口调用结束的回调函数(调用成功、失败都会执行) |
|
GetPrivacySettingSuccessResult 的属性值
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
| needAuthorization | boolean | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
| | x | 4.41 | 4.31 | 4.31 | 4.31 | 4.61 |
| 是否需要用户授权隐私协议(用户之前同意过返回false,没同意过则返回true) |
| privacyContractName | string | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | 4.41 | - | - | - |
| 隐私授权协议的名称
|
参见
uni.resetPrivacyAuthorization()
重置隐私协议状态为未同意
适用于隐私协议变更,需要重新同意的场景。
resetPrivacyAuthorization 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS |
| x | - | 4.31 | 4.31 | 4.31 | 4.61 |
参见
uni.onPrivacyAuthorizationChange(callback)
开启监听隐私协议状态改变
onPrivacyAuthorizationChange 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS |
| x | - | 4.31 | 4.31 | 4.31 | 4.61 |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| callback | (res: PrivacyChangeResult) => void | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| |
PrivacyChangeResult 的属性值
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
| needAuthorization | boolean | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
| | x | - | 4.31 | 4.31 | 4.31 | 4.61 |
| 是否需要用户授权隐私协议(用户之前同意过返回false,没同意过则返回true) |
返回值
参见
uni.offPrivacyAuthorizationChange(id)
取消监听隐私协议状态改变
offPrivacyAuthorizationChange 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS |
| x | - | 4.31 | 4.31 | 4.31 | 4.61 |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| id | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| 开启监听隐私协议状态改变返回的id |
参见
示例
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见
该 API 不支持 Web,请运行 hello uni-app x 到 App 平台体验
扫码体验 示例源码如下,请查看 pre > code 标签中的内容
<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>
通用类型
GeneralCallbackResult
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
| errMsg | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | 4.41 | - | - | - |
| 错误信息 |