提交 cb6fe157 编写于 作者: Z zhouyan

访问控制permissionrequestresult.md告警整改

Signed-off-by: Nzhouyan <zhouyan160@huawei.com>
Change-Id: Iaaa670feab210293cd181e57674b61b905ea455b
上级 7bad250e
...@@ -21,15 +21,65 @@ ...@@ -21,15 +21,65 @@
通过atManager实例来获取。 通过atManager实例来获取。
**示例:** **示例:**
ArkTS语法不支持直接使用globalThis,需要通过一个单例的map来做中转。开发者需要:
a. 在EntryAbility.ets中导入构建的单例对象GlobalThis。
```typescript
import { GlobalThis } from '../utils/globalThis'; // 需要根据globalThis.ets的路径自行适配
```
b. 在onCreate中添加:
```typescript
GlobalThis.getInstance().setContext('context', this.context);
```
> **说明:**
>
> 由于在ts中引入ets文件会有告警提示,需要将EntryAbility.ts的文件后缀修改为EntryAbility.ets,并在module.json5中同步修改。
**globalThis.ets示例代码如下:**
```typescript
import common from '@ohos.app.ability.common';
// 构造单例对象
export class GlobalThis {
private constructor() {}
private static instance: GlobalThis;
private _uiContexts = new Map<string, common.UIAbilityContext>();
public static getInstance(): GlobalThis {
if (!GlobalThis.instance) {
GlobalThis.instance = new GlobalThis();
}
return GlobalThis.instance;
}
getContext(key: string): common.UIAbilityContext | undefined {
return this._uiContexts.get(key);
}
setContext(key: string, value: common.UIAbilityContext): void {
this._uiContexts.set(key, value);
}
// 其他需要传递的内容依此扩展
}
```
```ts ```ts
import { BusinessError } from '@ohos.base';
import abilityAccessCtrl from '@ohos.abilityAccessCtrl'; import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
import common from '@ohos.app.ability.common';
import { GlobalThis } from '../utils/globalThis';
let atManager = abilityAccessCtrl.createAtManager(); let atManager = abilityAccessCtrl.createAtManager();
try { try {
atManager.requestPermissionsFromUser(this.context, ["ohos.permission.CAMERA"]).then((data) => { let context: common.UIAbilityContext = GlobalThis.getInstance().getContext('context');
atManager.requestPermissionsFromUser(context, ["ohos.permission.CAMERA"]).then((data) => {
console.info("data:" + JSON.stringify(data)); console.info("data:" + JSON.stringify(data));
console.info("data permissions:" + data.permissions); console.info("data permissions:" + data.permissions);
console.info("data authResults:" + data.authResults); console.info("data authResults:" + data.authResults);
}).catch((err) => { }).catch((err: BusinessError) => {
console.info("data:" + JSON.stringify(err)); console.info("data:" + JSON.stringify(err));
}) })
} catch(err) { } catch(err) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册