1# 证书管理对话框开发指导 2 3<!--Kit: Device Certificate Kit--> 4<!--Subsystem: Security--> 5<!--Owner: @chaceli--> 6<!--Designer: @chande--> 7<!--Tester: @zhangzhi1995--> 8<!--Adviser: @zengyawen--> 9 10> **说明** 11> 12> 本开发指导需使用API version 13及以上版本SDK。 13 14证书管理对话框,可用于拉起证书管理页面并管理证书,如安装、存储、使用、销毁证书。 15 16## 接口说明 17 18详细接口说明可参考[API参考](../../reference/apis-device-certificate-kit/js-apis-certManagerDialog.md)。 19 20以上场景涉及的常用接口如下表所示。 21 22| 实例名 | 接口名 | 描述 | 23| --------------- | ------------------------------------------------------------ | -------------------------------------------- | 24| certificateManagerDialog | openCertificateManagerDialog(context: common.Context, pageType: CertificateDialogPageType): Promise\<void> | 拉起证书管理对话框,显示相应的页面,使用Promise方式异步返回结果。 | 25| certificateManagerDialog | openInstallCertificateDialog(context: common.Context, certType: CertificateType, certScope: CertificateScope, cert: Uint8Array): Promise\<string><sup>14+</sup> | 调用安装证书对话框接口进行证书安装,使用Promise方式异步返回安装证书的唯一标识符。<br/>仅2in1设备支持。 | 26| certificateManagerDialog | openUninstallCertificateDialog(context: common.Context, certType: CertificateType, certUri: string): Promise\<void><sup>18+</sup> | 调用删除证书对话框接口删除指定的证书,使用Promise方式异步返回结果。<br/>仅2in1设备支持。 | 27| certificateManagerDialog | openCertificateDetailDialog(context: common.Context, cert: Uint8Array, property: CertificateDialogProperty): Promise\<void><sup>18+</sup> | 调用查看证书详情的对话框接口,展示证书的详情。使用Promise方式异步返回结果。<br/>仅2in1设备支持。 | 28 29## 开发步骤 30 311. 权限申请和声明。 32 33 需要申请的权限:ohos.permission.ACCESS_CERT_MANAGER 34 35 申请流程请参考:[申请应用权限](../AccessToken/determine-application-mode.md) 36 37 声明权限请参考:[声明权限](../AccessToken/declare-permissions.md) 38 392. 导入相关模块。 40 41 ```ts 42 import { certificateManagerDialog } from '@kit.DeviceCertificateKit'; 43 import { BusinessError } from '@kit.BasicServicesKit'; 44 import { common } from '@kit.AbilityKit'; 45 import { UIContext } from '@kit.ArkUI'; 46 ``` 47 483. 拉起证书管理界面。 49 50 ```ts 51 /* context为应用的上下文信息,由调用方自行获取,此处仅为示例。 */ 52 let context: common.Context = new UIContext().getHostContext() as common.Context; 53 async function certificateManagerDialogSample() { 54 /* pageType为页面类型,此处赋值PAGE_MAIN,即拉起证书管理主界面。 */ 55 let pageType: certificateManagerDialog.CertificateDialogPageType = certificateManagerDialog.CertificateDialogPageType.PAGE_MAIN; 56 try { 57 certificateManagerDialog.openCertificateManagerDialog(context, pageType).then(() => { 58 console.info('Succeeded in opening certificate manager dialog.'); 59 }).catch((err: BusinessError) => { 60 console.error(`Failed to open certificate manager dialog. Code: ${err.code}, message: ${err.message}`); 61 }) 62 } catch (error) { 63 console.error(`Failed to open certificate manager dialog. Code: ${error.code}, message: ${error.message}`); 64 } 65 } 66 ``` 674. 调用安装证书对话框接口进行证书安装、调用删除证书对话框接口进行证书删除、调用查看证书详情的对话框接口、展示证书的详情。以上场景仅2in1设备支持。 68 69 ```ts 70 /* context为应用的上下文信息,由调用方自行获取,此处仅为示例。 */ 71 let context: common.Context = new UIContext().getHostContext() as common.Context; 72 async function userCADialogSample() { 73 let certUri: string = ''; 74 let certType = certificateManagerDialog.CertificateType.CA_CERT; 75 /* 用户CA证书数据需要业务赋值。 */ 76 let cert = new Uint8Array([ 77 0x30, 0x82, 0x01, 0x2E, 0x30, 0x81, 0xD5, 0x02, 0x14, 0x28, 0x75, 0x71, 0x22, 0xDF, 0xDC, 0xCB, 78 ]); 79 80 try { 81 /* 安装证书。 */ 82 let certScope = certificateManagerDialog.CertificateScope.CURRENT_USER; /* 安装在当前用户下。 */ 83 certificateManagerDialog.openInstallCertificateDialog(context, certType, certScope, cert).then((result) => { 84 console.info('Succeeded in opening install ca dialog.'); 85 certUri = result; 86 }).catch((err: BusinessError) => { 87 console.error(`Failed to open install ca dialog. Code: ${err.code}, message: ${err.message}`); 88 }) 89 } catch (error) { 90 console.error(`Failed to open install ca dialog. Code: ${error.code}, message: ${error.message}`); 91 } 92 93 try { 94 /* 删除证书。 */ 95 certificateManagerDialog.openUninstallCertificateDialog(context, certType, certUri).then(() => { 96 console.info('Succeeded in opening uninstall ca dialog.'); 97 }).catch((err: BusinessError) => { 98 console.error(`Failed to open uninstall ca dialog. Code: ${err.code}, message: ${err.message}`); 99 }) 100 } catch (error) { 101 console.error(`Failed to open uninstall ca dialog. Code: ${error.code}, message: ${error.message}`); 102 } 103 104 try { 105 let property: certificateManagerDialog.CertificateDialogProperty = { 106 showInstallButton: false /* 不显示安装按钮。 */ 107 }; 108 /* 显示证书详情。 */ 109 certificateManagerDialog.openCertificateDetailDialog(context, cert, property).then(() => { 110 console.info('Succeeded in opening show ca detail dialog.'); 111 }).catch((err: BusinessError) => { 112 console.error(`Failed to open show ca detail dialog. Code: ${err.code}, message: ${err.message}`); 113 }) 114 } catch (error) { 115 console.error(`Failed to open show ca detail dialog. Code: ${error.code}, message: ${error.message}`); 116 } 117 } 118 ``` 119