openharmony 鸿蒙 arkts-apis-webview-GeolocationPermissions

2026-08-25 浏览 (1)

Class (GeolocationPermissions)

Implements a GeolocationPermissions object.

NOTE

  • The initial APIs of this module are supported since API version 9. Updates will be marked with a superscript to indicate their earliest API version.

  • The initial APIs of this class are supported since API version 9.

  • The sample effect is subject to the actual device.

  • You must load the Web component before calling the APIs in GeolocationPermissions.

Modules to Import

import { webview } from '@kit.ArkWeb';

Required Permissions

ohos.permission.LOCATION, ohos.permission.APPROXIMATELY_LOCATION, and ohos.permission.LOCATION_IN_BACKGROUND, which are required for accessing the location information. For details about the permissions, see @ohos.geolocation (Geolocation).

allowGeolocation

static allowGeolocation(origin: string, incognito?: boolean): void

Allows the specified origin to use the geolocation information.

System capability: SystemCapability.Web.Webview.Core

Parameters

NameTypeMandatoryDescription
originstringYesIndex of the origin.
incognito11+booleanNoWhether to allow the specified origin to use the geolocation information in incognito mode. The value true means to allow the specified origin to use the geolocation information in incognito mode, and false means the opposite.
Default value: false.
If null or undefined is passed in, the value is false.

Error codes

For details about the error codes, see Webview Error Codes and Universal Error Codes.

IDError Message
17100011Invalid origin.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.

Example

// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct WebComponent {
  controller: webview.WebviewController = new webview.WebviewController();
  origin: string = "file:///";

  build() {
    Column() {
      Button('allowGeolocation')
        .onClick(() => {
          try {
            webview.GeolocationPermissions.allowGeolocation(this.origin);
          } catch (error) {
            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
          }
        })
      Web({ src: 'www.example.com', controller: this.controller })
    }
  }
}

deleteGeolocation

static deleteGeolocation(origin: string, incognito?: boolean): void

Clears the geolocation permission status of a specified origin.

System capability: SystemCapability.Web.Webview.Core

Parameters

NameTypeMandatoryDescription
originstringYesIndex of the origin.
incognito11+booleanNoWhether to clear the geolocation permission status of a specified origin in incognito mode. The value true means to clear the geolocation permission status of a specified origin in incognito mode, and false means the opposite.
Default value: false.
If null or undefined is passed in, the value is false.

Error codes

For details about the error codes, see Webview Error Codes and Universal Error Codes.

IDError Message
17100011Invalid origin.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.

Example

// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct WebComponent {
  controller: webview.WebviewController = new webview.WebviewController();
  origin: string = "file:///";

  build() {
    Column() {
      Button('deleteGeolocation')
        .onClick(() => {
          try {
            webview.GeolocationPermissions.deleteGeolocation(this.origin);
          } catch (error) {
            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
          }
        })
      Web({ src: 'www.example.com', controller: this.controller })
    }
  }
}

getAccessibleGeolocation

static getAccessibleGeolocation(origin: string, callback: AsyncCallback<boolean>, incognito?: boolean): void

Obtains the geolocation permission status of the specified origin. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Web.Webview.Core

Parameters

NameTypeMandatoryDescription
originstringYesIndex of the origin.
callbackAsyncCallback<boolean>YesCallback used to return the geolocation permission status of the specified origin.
If the operation is successful, the value true means that the geolocation permission is granted, and false means the opposite.
If the operation fails, the geolocation permission status of the specified origin is not found.
incognito11+booleanNoWhether to obtain the geolocation permission status of the specified origin in incognito mode. The value true means to obtain the geolocation permission status of the specified origin in incognito mode, and false means the opposite.
Default value: false.
If null or undefined is passed, error code 401 is thrown.

Error codes

For details about the error codes, see Webview Error Codes and Universal Error Codes.

IDError Message
17100011Invalid origin.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.

Example

// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct WebComponent {
  controller: webview.WebviewController = new webview.WebviewController();
  origin: string = "file:///";

  build() {
    Column() {
      Button('getAccessibleGeolocation')
        .onClick(() => {
          try {
            webview.GeolocationPermissions.getAccessibleGeolocation(this.origin, (error, result) => {
              if (error) {
                console.error(`getAccessibleGeolocationAsync error, ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
                return;
              }
              console.info('getAccessibleGeolocationAsync result: ' + result);
            });
          } catch (error) {
            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
          }
        })
      Web({ src: 'www.example.com', controller: this.controller })
    }
  }
}

getAccessibleGeolocation

static getAccessibleGeolocation(origin: string, incognito?: boolean): Promise<boolean>

Obtains the geolocation permission status of the specified origin. This API uses a promise to return the result.

System capability: SystemCapability.Web.Webview.Core

Parameters

NameTypeMandatoryDescription
originstringYesIndex of the origin.
incognito11+booleanNoWhether to obtain the geolocation permission status of the specified origin in incognito mode. The value true means to obtain the geolocation permission status of the specified origin in incognito mode, and false means the opposite.
Default value: false.
If null or undefined is passed, error code 401 is thrown.

Return value

TypeDescription
Promise<boolean>Promise used to return the geolocation permission status of the specified origin.
If the operation is successful, the value true means that the geolocation permission is granted, and false means the opposite.
If the operation fails, the geolocation permission status of the specified origin is not found.

Error codes

For details about the error codes, see Webview Error Codes and Universal Error Codes.

IDError Message
17100011Invalid origin.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.

Example

// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct WebComponent {
  controller: webview.WebviewController = new webview.WebviewController();
  origin: string = "file:///";

  build() {
    Column() {
      Button('getAccessibleGeolocation')
        .onClick(() => {
          try {
            webview.GeolocationPermissions.getAccessibleGeolocation(this.origin)
              .then(result => {
                console.info('getAccessibleGeolocationPromise result: ' + result);
              }).catch((error: BusinessError) => {
              console.error(`getAccessibleGeolocationPromise error, ErrorCode: ${error.code},  Message: ${error.message}`);
            });
          } catch (error) {
            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
          }
        })
      Web({ src: 'www.example.com', controller: this.controller })
    }
  }
}

getStoredGeolocation

static getStoredGeolocation(callback: AsyncCallback<Array<string>>, incognito?: boolean): void

Obtains the geolocation permission status of all origins. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Web.Webview.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<Array<string>>YesCallback used to return the geolocation permission status of all origins.
incognito11+booleanNoWhether to obtain the geolocation permission status of all origins in incognito mode. The value true means to obtain the geolocation permission status of all origins in incognito mode, and false means the opposite.
Default value: false.
If null or undefined is passed, error code 401 is thrown.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.

Example

// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct WebComponent {
  controller: webview.WebviewController = new webview.WebviewController();

  build() {
    Column() {
      Button('getStoredGeolocation')
        .onClick(() => {
          try {
            webview.GeolocationPermissions.getStoredGeolocation((error, origins) => {
              if (error) {
                console.error(`getStoredGeolocationAsync error, ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
                return;
              }
              let origins_str: string = origins.join();
              console.info('getStoredGeolocationAsync origins: ' + origins_str);
            });
          } catch (error) {
            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
          }
        })
      Web({ src: 'www.example.com', controller: this.controller })
    }
  }
}

getStoredGeolocation

static getStoredGeolocation(incognito?: boolean): Promise<Array<string>>

Obtains the geolocation permission status of all origins. This API uses a promise to return the result.

System capability: SystemCapability.Web.Webview.Core

Parameters

NameTypeMandatoryDescription
incognito11+booleanNoWhether to obtain the geolocation permission status of all origins in incognito mode. The value true means to obtain the geolocation permission status of all origins in incognito mode, and false means the opposite.
Default value: false.
If null or undefined is passed, error code 401 is thrown.

Return value

TypeDescription
Promise<Array<string>>Promise used to return the geolocation permission status of all origins.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.

Example

// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct WebComponent {
  controller: webview.WebviewController = new webview.WebviewController();

  build() {
    Column() {
      Button('getStoredGeolocation')
        .onClick(() => {
          try {
            webview.GeolocationPermissions.getStoredGeolocation()
              .then(origins => {
                let origins_str: string = origins.join();
                console.info('getStoredGeolocationPromise origins: ' + origins_str);
              }).catch((error: BusinessError) => {
              console.error(`getStoredGeolocationPromise error, ErrorCode: ${error.code},  Message: ${error.message}`);
            });
          } catch (error) {
            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
          }
        })
      Web({ src: 'www.example.com', controller: this.controller })
    }
  }
}

deleteAllGeolocation

static deleteAllGeolocation(incognito?: boolean): void

Clears the geolocation permission status of all sources.

System capability: SystemCapability.Web.Webview.Core

Parameters

NameTypeMandatoryDescription
incognito11+booleanNoWhether to clear the geolocation permission status of all sources in incognito mode. The value true means to clear the geolocation permission status of all sources in incognito mode, and false means the opposite.
Default value: false.
If null or undefined is passed in, the value is false.

Example

// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct WebComponent {
  controller: webview.WebviewController = new webview.WebviewController();

  build() {
    Column() {
      Button('deleteAllGeolocation')
        .onClick(() => {
          try {
            webview.GeolocationPermissions.deleteAllGeolocation();
          } catch (error) {
            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
          }
        })
      Web({ src: 'www.example.com', controller: this.controller })
    }
  }
}

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 arkts-apis-webview-WebviewController

openharmony 鸿蒙 arkts-apis-webview-t

openharmony 鸿蒙 arkts-apis-webview

openharmony 鸿蒙 capi-arkweb-type-h

openharmony 鸿蒙 arkts-basic-components-web-FullScreenExitHandler

openharmony 鸿蒙 arkts-apis-webview-WebDownloadManager

openharmony 鸿蒙 arkts-basic-components-web-VerifyPinHandler

openharmony 鸿蒙 capi-web-arkweb-webmessage8h

openharmony 鸿蒙 arkts-apis-webview-NativeMediaPlayerHandler

openharmony 鸿蒙 capi-web-arkweb-proxymethodwithresult

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/SYTZ4EaC