HarmonyOS鸿蒙Next中【快应用】小程序转快应用中如何获取用户已授权的权限

HarmonyOS鸿蒙Next中【快应用】小程序转快应用中如何获取用户已授权的权限 【关键词】

  • 权限
  • SystemInfo
  • setting

【问题背景】

小程序转快应用,用户在使用快应用的过程中如果产生了一些授权行为,开发者是否有办法去收集到用户已经授权过的权限呢,从而进行更好管理呢?

【解决方案】

小程序转快应用中是可行的,方法有如下两种:

  1. 通过 qa.getSystemInfoSync() 接口返回的信息。如下:
getset2() {
  const sysInfo = qa.getSystemInfoSync()
  console.log(sysInfo)
}

运行结果:

{
  "devicePixelRatio": 3,
  "albumAuthorized": false,
  "cameraAuthorized": false,
  "locationAuthorized": false,
  "microphoneAuthorized": false,
  "notificationAuthorized": false,
  "notificationAlertAuthorized": false,
  "notificationBadgeAuthorized": false,
  "notificationSoundAuthorized": false,
  "bluetoothEnabled": false,
  "locationEnabled": true,
  "wifiEnabled": true
}
  1. 通过 qa.getSetting 获取当前用户的设置。
qa.getSetting({
  success(res) {
    console.log("getSetting success, authSetting = " + JSON.stringify(res.authSetting))
  }
})

运行结果:

getSetting success, authSetting = {
  "scope.userLocation": false,
  "scope.writePhotosAlbum": true,
  "scope.record": false,
  "scope.camera": false,
  "scope.userInfo": false,
  "scope.address": false,
  "scope.invoiceTitle": false,
  "scope.invoice": false,
  "scope.werun": false
}

更多关于HarmonyOS鸿蒙Next中【快应用】小程序转快应用中如何获取用户已授权的权限的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于HarmonyOS鸿蒙Next中【快应用】小程序转快应用中如何获取用户已授权的权限的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,将小程序转为快应用时,可以通过permission模块获取用户已授权的权限。使用permission.getPermissions()方法,传入权限列表,系统会返回相应的授权状态。具体步骤如下:

  1. 导入permission模块:import permission from '@ohos.permission';

  2. 调用getPermissions()方法,传入权限列表,如permission.getPermissions(['permission1', 'permission2'])

  3. 处理返回的授权状态,判断用户是否已授权。

回到顶部