DEV Community

桃花镇童长老
桃花镇童长老

Posted on

PermissionUtil, authorization related tool classes

Introduction and description of harmony-utils


harmony-utils A HarmonyOS tool library with rich features and extremely easy to use, with the help of many practical tools, is committed to helping developers quickly build Hongmeng applications. Its encapsulated tools cover APP, device, screen, authorization, notification, inter-thread communication, pop-up frames, toast, biometric authentication, user preferences, taking photos, albums, scanning codes, files, logs, exception capture, characters, strings, numbers, collections, dates, random, base64, encryption, decryption, JSON and other functions, which can meet various development needs.

picker_utils It is a sub-store split by harmony-utils, including PickerUtil, PhotoHelper, and ScanUtil.

Download and install

ohpm i @pura/harmony-utils

ohpm i @pura/picker_utils

  //Global initialization method, initialized in the onCreate method of UIAbility AppUtil.init()
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    AppUtil.init(this.context);
  }
Enter fullscreen mode Exit fullscreen mode

API methods and usage


requestPermissionsEasy Apply for authorization, after rejection, and apply for authorization to the user again (apply for permission, it is recommended to use this method)
let p: Permissions[] = ['ohos.permission.ACTIVITY_MOTION', 'ohos.permission.CAMERA', 'ohos.permission.LOCATION', 'ohos.permission.APPROXIMATELY_LOCATION'];
PermissionUtil.requestPermissionsEasy(p).then((result) => {
  ToastUtil.showToast(`申请授权,结果:${result}`);
})
Enter fullscreen mode Exit fullscreen mode
checkPermissions Check if it is currently authorized
let p: Permissions = 'ohos.permission.CAMERA'; //相机
PermissionUtil.checkPermissions(p).then((result) => {
ToastUtil.showToast(`检测是否授权,结果:${result}`);
})
Enter fullscreen mode Exit fullscreen mode
checkRequestPermissions After verifying whether authorization is authorized and apply for authorization
let p: Permissions = 'ohos.permission.CAMERA'; //相机
PermissionUtil.checkRequestPermissions(p).then((grant) => {
  ToastUtil.showToast(`检测并申请授权,结果:${grant}`);
  if (!grant) {
    WantUtil.toAppSetting(); //拒绝权限,跳转APP设置页面
  }
})
Enter fullscreen mode Exit fullscreen mode
requestPermissions
let p: Permissions[] = ['ohos.permission.ACTIVITY_MOTION', 'ohos.permission.CAMERA','ohos.permission.LOCATION', 'ohos.permission.APPROXIMATELY_LOCATION'];
PermissionUtil.requestPermissions(p).then((grant) => {
  if (grant) {
    ToastUtil.showToast(`申请授权,已通过...`);
  } else { //拒绝权限,二次向用户申请授权
    PermissionUtil.requestPermissionOnSettingEasy(p).then((result) => {
      ToastUtil.showToast(`申请授权,结果:${result}`);
    });
  }
})
Enter fullscreen mode Exit fullscreen mode
requestPermissionOnSetting Secondary application for authorization to the user (single permissions or read and write permission groups, this method is recommended)
let ps: Permissions[] = ['ohos.permission.READ_IMAGEVIDEO', 'ohos.permission.WRITE_IMAGEVIDEO'];
PermissionUtil.requestPermissions(ps).then((result) => {
  if (result) {
    ToastUtil.showToast(`最佳使用案例授权,已通过...`);
  } else {
    PermissionUtil.requestPermissionOnSetting(ps).then((grant) => {
      ToastUtil.showToast(`最佳使用案例,结果:${grant}`);
    })
  }
})
Enter fullscreen mode Exit fullscreen mode
requestPermissionOnSettingEasy Secondary application for authorization to the user (this method is recommended for multiple permissions)
let p: Permissions[] = ['ohos.permission.ACTIVITY_MOTION', 'ohos.permission.CAMERA','ohos.permission.LOCATION', 'ohos.permission.APPROXIMATELY_LOCATION'];
PermissionUtil.requestPermissions(p).then((grant) => {
  if (grant) {
    ToastUtil.showToast(`申请授权,已通过...`);
  } else { //拒绝权限,二次向用户申请授权
    PermissionUtil.requestPermissionOnSettingEasy(p).then((result) => {
      ToastUtil.showToast(`申请授权,结果:${result}`);
    });
  }
})
Enter fullscreen mode Exit fullscreen mode
requestGlobalSwitch is used for UIAbility/UIExtensionAbility to pull up the global switch to set the pop-up box. In some cases, recording, taking photos and other functions are disabled. The application can pull up this box and ask the user to agree to enable the corresponding functions. If the current global switch is on, the pop-up box will not be pulled
PermissionUtil.requestGlobalSwitch(abilityAccessCtrl.SwitchType.LOCATION).then((result) => {
  ToastUtil.showToast(`申请结果:${result}`);
}).catch((err: BusinessError) => {
  ToastUtil.showToast(err.message);
  LogUtil.error(err);
});
Enter fullscreen mode Exit fullscreen mode

Top comments (0)