docs/api/privacy.md
因为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::: sourceCode
GitCode: https://gitcode.com/dcloud/uni-api/tree/alpha/uni_modules/uni-privacy
GitHub: https://github.com/dcloudio/uni-api/tree/alpha/uni_modules/uni-privacy
:::
获取隐私协议状态
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|---|
| <a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a> | 4.41 | 4.31 | 4.31 | 4.31 | 4.61 | 5.0 |
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| options | GetPrivacySettingOptions | 是 | - | Web: x; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| success | (result: GetPrivacySettingSuccessResult) => void | 否 | null | Web: x; 微信小程序: 4.41; Android: 4.31; iOS: 4.31; iOS uni-app x UTS 插件: 4.31; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 接口调用成功的回调函数 |
| fail | (result: any) => void | 否 | null | Web: x; 微信小程序: 4.41; Android: 4.31; iOS: 4.31; iOS uni-app x UTS 插件: 4.31; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 接口调用失败的回调函数 |
| complete | (result: any) => void | 否 | null | Web: x; 微信小程序: 4.41; Android: 4.31; iOS: 4.31; iOS uni-app x UTS 插件: 4.31; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 接口调用结束的回调函数(调用成功、失败都会执行) |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| needAuthorization | boolean | 是 | - | Web: x; 微信小程序: 4.41; Android: 4.31; iOS: 4.31; iOS uni-app x UTS 插件: 4.31; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 是否需要用户授权隐私协议(用户之前同意过返回false,没同意过则返回true) |
| privacyContractName | string | 否 | - | Web: x; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 隐私授权协议的名称 |
::: sourceCode
GitCode: https://gitcode.com/dcloud/uni-api/tree/alpha/uni_modules/uni-privacy
GitHub: https://github.com/dcloudio/uni-api/tree/alpha/uni_modules/uni-privacy
:::
重置隐私协议状态为未同意
适用于隐私协议变更,需要重新同意的场景。
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|---|
| <a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a> | - | 4.31 | 4.31 | 4.31 | 4.61 | 5.0 |
::: sourceCode
GitCode: https://gitcode.com/dcloud/uni-api/tree/alpha/uni_modules/uni-privacy
GitHub: https://github.com/dcloudio/uni-api/tree/alpha/uni_modules/uni-privacy
:::
开启监听隐私协议状态改变
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|---|
| <a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a> | - | 4.31 | 4.31 | 4.31 | 4.61 | 5.0 |
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| callback | (res: PrivacyChangeResult) => void | 是 | - | Web: x; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| needAuthorization | boolean | 是 | - | Web: x; 微信小程序: -; Android: 4.31; iOS: 4.31; iOS uni-app x UTS 插件: 4.31; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 是否需要用户授权隐私协议(用户之前同意过返回false,没同意过则返回true) |
| 类型 |
|---|
| number |
::: sourceCode
GitCode: https://gitcode.com/dcloud/uni-api/tree/alpha/uni_modules/uni-privacy
GitHub: https://github.com/dcloudio/uni-api/tree/alpha/uni_modules/uni-privacy
:::
取消监听隐私协议状态改变
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|---|
| <a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a> | - | 4.31 | 4.31 | 4.31 | 4.61 | 5.0 |
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| id | number | 是 | - | Web: x; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | 开启监听隐私协议状态改变返回的id |
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见
该 API 不支持 Web,请运行 hello uni-app x 到 App 平台体验
::: preview
appRedirect https://hellouniappx.dcloud.net.cn/appredirect.html?path=pages/API/privacy/privacy
<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 setup lang="uts">
const title = ref('隐私信息授权')
const appPrivacy = ref('未获取')
const privacyContractName = ref("")
const listenId = ref(0)
const getPrivacySetting = () => {
uni.getPrivacySetting({
success: (res) => {
appPrivacy.value = res.needAuthorization ? "未同意" : "已同意"
// #ifdef MP-WEIXIN
privacyContractName.value = res.privacyContractName
// #endif
}
})
}
const resetPrivacyAuthorization = () => {
uni.resetPrivacyAuthorization()
}
const openPrivacyDialog = () => {
uni.openDialogPage({
url: '/pages/component/button/privacy',
})
}
onReady(() => {
// #ifdef APP
//添加 隐私协议监听
const id = uni.onPrivacyAuthorizationChange((res) => {
appPrivacy.value = res.needAuthorization ? "未同意" : "已同意"
const privacyState = "监听到隐私协议状态已变更为 " + appPrivacy.value;
uni.showToast({
"position": "bottom",
"title": privacyState
})
})
listenId.value = id;
uni.showToast({
"position": "bottom",
"title": "开启监听隐私协议状态"
})
// #endif
})
onUnload(() => {
// #ifdef APP
//注销监听
uni.offPrivacyAuthorizationChange(listenId.value)
listenId.value = 0;
uni.showToast({
"position": "bottom",
"title": "已停止监听隐私协议状态"
})
// #endif
})
</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 | 是 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 错误信息 |