harmony 鸿蒙@ohos.net.vpnExtension (Enhanced VPN Management) (System API)

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

@ohos.net.vpnExtension (Enhanced VPN Management) (System API)

This module implements virtual private network (VPN) management, such as starting and stopping a third-party VPN. Third-party VPNs refer to VPN services provided by third parties. They usually support more security and privacy functions and more comprehensive customization options.

NOTE The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version. This topic describes only system APIs provided by the module. For details about its public APIs, see @ohos.net.vpnExtension (VPN Extension).

Modules to Import

import { vpnExtension } from '@kit.NetworkKit';

vpnExtension.setAlwaysOnVpnEnabled

setAlwaysOnVpnEnabled(enable: boolean, bundleName: string): Promise<void>

Enables or disables the always on mode.

System API: This is a system API.

Required permissions: ohos.permission.MANAGE_VPN

System capability: SystemCapability.Communication.NetManager.Vpn

Model restriction: This API can be used only in the stage model.

Parameters

Name Type Mandatory Description
enable boolean Yes Whether to enable the always on mode.
bundleName string Yes Bundle name of the application (generally a third-party application).

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

ID Error Message
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.

Example Stage model:

import { vpnExtension } from '@kit.NetworkKit';
import { Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

let want: Want = {
  deviceId: "",
  bundleName: 'com.example.myvpndemo',
  abilityName: 'MyVpnExtAbility',
};

vpnExtension.setAlwaysOnVpnEnabled(true, want.bundleName).then(() => {
  console.info('setAlwaysOnVpnEnabled success.');
}).catch((err : BusinessError) => {
  console.error('setAlwaysOnVpnEnabled fail, err-> ${JSON.stringify(err)}');
});

vpnExtension.isAlwaysOnVpnEnabled

isAlwaysOnVpnEnabled(bundleName: string): Promise<boolean>

Obtains the status of the always on mode.

System API: This is a system API.

Required permissions: ohos.permission.MANAGE_VPN

System capability: SystemCapability.Communication.NetManager.Vpn

Model restriction: This API can be used only in the stage model.

Parameters

Name Type Mandatory Description
bundleName string Yes Bundle name of the application (generally a third-party application).

Return value

Type Description
Promise<boolean> Promise used to return the result.

Error codes

ID Error Message
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.

Example Stage model:

import { vpnExtension } from '@kit.NetworkKit';
import { Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

let want: Want = {
  deviceId: "",
  bundleName: 'com.example.myvpndemo',
  abilityName: 'MyVpnExtAbility',
};

vpnExtension.isAlwaysOnVpnEnabled(want.bundleName).then((data : boolean) => {
  console.info('isAlwaysOnVpnEnabled success.');
}).catch((err : BusinessError) => {
  console.error('setAlwaysOnVpnEnabled fail, err-> ${JSON.stringify(err)}');
});

vpnExtension.updateVpnAuthorizedState

updateVpnAuthorizedState(bundleName: string): boolean

Updates the VPN pop-up authorization status.

System API: This is a system API.

Required permissions: ohos.permission.MANAGE_VPN

System capability: SystemCapability.Communication.NetManager.Vpn

Model restriction: This API can be used only in the stage model.

Parameters

Name Type Mandatory Description
bundleName string Yes Bundle name of the application (generally a third-party application).

Return value

Type Description
boolean Boolean value indicating whether the VPN pop-up authorization status is successfully updated.

Error codes

ID Error Message
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.

Example Stage model:

import { vpnExtension } from '@kit.NetworkKit';
import { Want } from '@kit.AbilityKit';

let want: Want = {
  deviceId: "",
  bundleName: 'com.example.myvpndemo',
  abilityName: 'MyVpnExtAbility',
};

let result: boolean = vpnExtension.updateVpnAuthorizedState(want.bundleName);
console.log("Result: "+ result);

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Network Kit

harmony 鸿蒙NetConn_ConnectionProperties

harmony 鸿蒙NetConn_HttpProxy

harmony 鸿蒙NetConn_NetAddr

harmony 鸿蒙NetConn_NetCapabilities

harmony 鸿蒙NetConn_NetConnCallback

harmony 鸿蒙NetConn_NetHandle

harmony 鸿蒙NetConn_NetHandleList

harmony 鸿蒙NetConn_NetSpecifier

harmony 鸿蒙NetConn_Route

0  赞