harmony 鸿蒙PermissionRequestResult

  • 2025-06-12
  • 浏览 (5)

PermissionRequestResult

The PermissionRequestResult module defines the permission request result returned by requestPermissionsFromUser.

NOTE

  • The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
  • The APIs of this module can be used only in the stage model.

Properties

System capability: SystemCapability.Security.AccessToken

Name Type Read Only Optional Description
permissions Array<string> Yes No Permissions requested.
Atomic service API: This API can be used in atomic services since API version 11.
authResults Array<number> Yes No Result of the permission request.
- -1: The permission is not granted. If dialogShownResults is true, it is the first time that the user requests the permission. If dialogShownResults is false, the permission has been set and no dialog box is displayed. The user can modify the permission settings in Settings.
- 0: The permission is granted.
- 2: The permission request is invalid. The possible causes are as follows: 1. The permission is not declared in the configuration file. 2. The permission name is invalid. 3. The conditions for requesting the permission are not met. For details, see ohos.permission.LOCATION and ohos.permission.APPROXIMATELY_LOCATION.
Atomic service API: This API can be used in atomic services since API version 11.
dialogShownResults12+ Array<boolean> Yes Yes Whether to display a dialog box.
The value true means to display a dialog box; the value false means the opposite.
Atomic service API: This API can be used in atomic services since API version 12.
errorReasons18+ Array<number> Yes Yes Return value.
- 0: The request is valid.
- 1: The permission name is invalid.
- 2: The permission is not declared in the configuration file.
- 3: The conditions for requesting the permission are not met. For details, see the permission description in Permission List. Currently, only the location permissions are involved.
- 4: The user does not agree to the privacy statement.
- 5: This permission cannot be requested via a dialog box.
- 12: The service is abnormal.
Atomic service API: This API can be used in atomic services since API version 18.

Usage

The permission request result is obtained through an atManager instance.

Example For details about how to obtain the context in the example, see Obtaining the Context of UIAbility.

import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
import { BusinessError } from '@ohos.base';
import common from '@ohos.app.ability.common';

let atManager = abilityAccessCtrl.createAtManager();
try {
  let context: Context = this.getUIContext().getHostContext() as common.UIAbilityContext;
  atManager.requestPermissionsFromUser(context, ["ohos.permission.CAMERA"]).then((data) => {
      console.info("data:" + JSON.stringify(data));
      console.info("data permissions:" + data.permissions);
      console.info("data authResults:" + data.authResults);
      console.info("data dialogShownResults:" + data.dialogShownResults);
      console.info("data errorReasons:" + data.errorReasons);
  }).catch((err: BusinessError) => {
      console.error("data:" + JSON.stringify(err));
  })
} catch(err) {
  console.error(`catch err->${JSON.stringify(err)}`);
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Ability Kit

harmony 鸿蒙AbilityAccessControl

harmony 鸿蒙AbilityBase

harmony 鸿蒙AbilityBase_Element

harmony 鸿蒙AbilityRuntime

harmony 鸿蒙bundle

harmony 鸿蒙OH_NativeBundle_ApplicationInfo

harmony 鸿蒙OH_NativeBundle_ElementName

harmony 鸿蒙ability_access_control.h

harmony 鸿蒙ability_base_common.h

0  赞