1e41f4b71Sopenharmony_ci# @ohos.advertising (Ads Service Framework)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciThe advertising module provides APIs for requesting and displaying ads.
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci> **NOTE**
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci> 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.
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci## Modules to Import
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci```ts
15e41f4b71Sopenharmony_ciimport { advertising } from '@kit.AdsKit';
16e41f4b71Sopenharmony_ci```
17e41f4b71Sopenharmony_ci## AdLoader
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ciProvides the APIs for loading ads.
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Advertising.Ads
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci### constructor
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ciconstructor(context: common.Context);
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ciConstructor.
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Advertising.Ads
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci**Parameters**
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
38e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
39e41f4b71Sopenharmony_ci| context | common.[Context](../apis-ability-kit/js-apis-inner-application-context.md) | Yes| Context of the ability or application.|
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci**Example**
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ciFor details about how to obtain the context, see [Context](../../application-models/application-context-stage.md#overview).
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci```ts
46e41f4b71Sopenharmony_ciimport { advertising } from '@kit.AdsKit';
47e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_cifunction createConstructor(context: common.Context): void {
50e41f4b71Sopenharmony_ci  const load: advertising.AdLoader = new advertising.AdLoader(context);
51e41f4b71Sopenharmony_ci}
52e41f4b71Sopenharmony_ci```
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci### loadAd
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ciloadAd(adParam: AdRequestParams, adOptions: AdOptions, listener: AdLoadListener): void
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ciLoads an ad.
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Advertising.Ads
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ci**Parameters**
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 
68e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
69e41f4b71Sopenharmony_ci| adParam | [AdRequestParams](#adrequestparams) | Yes| Ad request parameters.| 
70e41f4b71Sopenharmony_ci| adOptions | [AdOptions](#adoptions) | Yes| Ad configuration.| 
71e41f4b71Sopenharmony_ci| listener | [AdLoadListener](#adloadlistener) | Yes| Ad request callback.| 
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ci**Error codes**
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ciFor details about the following error codes, see [Ads Service Framework Error Codes](errorcode-ads.md).
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci| ID| Error Message| 
78e41f4b71Sopenharmony_ci| -------- | -------- |
79e41f4b71Sopenharmony_ci| 401      | Invalid input parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 
80e41f4b71Sopenharmony_ci| 801      | Device not supported.  |
81e41f4b71Sopenharmony_ci| 21800001 | System internal error. | 
82e41f4b71Sopenharmony_ci| 21800003 | Failed to load the ad request. | 
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci**Example**
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ciFor details about how to obtain the context, see [Context](../../application-models/application-context-stage.md#overview).
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci```ts
90e41f4b71Sopenharmony_ciimport { advertising } from '@kit.AdsKit';
91e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
92e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit';
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_cifunction requestAd(context: common.Context): void {
95e41f4b71Sopenharmony_ci  const adRequestParam: advertising.AdRequestParams = {
96e41f4b71Sopenharmony_ci    // Ad type.
97e41f4b71Sopenharmony_ci    adType: 3,
98e41f4b71Sopenharmony_ci    // Ad ID.
99e41f4b71Sopenharmony_ci    adId: "testy63txaom86"
100e41f4b71Sopenharmony_ci  };
101e41f4b71Sopenharmony_ci  const adOptions: advertising.AdOptions = {
102e41f4b71Sopenharmony_ci    // Optional custom parameter, which specifies whether the ad can be downloaded while mobile data is in use. The value 1 means that the ad can be downloaded while mobile data is in use, and 0 means the opposite.
103e41f4b71Sopenharmony_ci    allowMobileTraffic: 0,
104e41f4b71Sopenharmony_ci    // Set the maximum ad content rating. The following values are available: w: ages 3+, all audiences; PI: ages 7+, audiences under parental instruction; J: ages 12+, teenagers; A: ages 16+/18+, adults.
105e41f4b71Sopenharmony_ci    adContentClassification: 'A',
106e41f4b71Sopenharmony_ci    // Specify whether you want your ad content to be treated as COPPA-compliant. The following values are available: -1 (default value): uncertain; 0: no; 1: yes.
107e41f4b71Sopenharmony_ci    tagForChildProtection: -1,
108e41f4b71Sopenharmony_ci    // Specify whether you want the ad request to be processed in a way that meets the GDPR for users in the EEA under the age of consent. The following values are available: -1 (default value): uncertain; 0: no; 1: yes.
109e41f4b71Sopenharmony_ci    tagForUnderAgeOfPromise: -1
110e41f4b71Sopenharmony_ci  }
111e41f4b71Sopenharmony_ci  // Listener for the ad loading status.
112e41f4b71Sopenharmony_ci  const adLoaderListener: advertising.AdLoadListener = {
113e41f4b71Sopenharmony_ci    // Called when the ad request fails.
114e41f4b71Sopenharmony_ci    onAdLoadFailure: (errorCode: number, errorMsg: string) => {
115e41f4b71Sopenharmony_ci      hilog.error(0x0000, 'testTag', '%{public}s',
116e41f4b71Sopenharmony_ci        `request single ad errorCode is: ${errorCode}, errorMsg is: ${errorMsg}`);
117e41f4b71Sopenharmony_ci    },
118e41f4b71Sopenharmony_ci    // Called when the ad request is successful.
119e41f4b71Sopenharmony_ci    onAdLoadSuccess: (ads: Array<advertising.Advertisement>) => {
120e41f4b71Sopenharmony_ci      hilog.info(0x0000, 'testTag', '%{public}s', 'succeed in requesting single ad!');
121e41f4b71Sopenharmony_ci      // Save the requested ad content for display.
122e41f4b71Sopenharmony_ci      const returnAds = ads;
123e41f4b71Sopenharmony_ci    }
124e41f4b71Sopenharmony_ci  };
125e41f4b71Sopenharmony_ci  // Create an AdLoader object.
126e41f4b71Sopenharmony_ci  const load: advertising.AdLoader = new advertising.AdLoader(context);
127e41f4b71Sopenharmony_ci  // Load the ad.
128e41f4b71Sopenharmony_ci  hilog.info(0x0000, 'testTag', '%{public}s', 'request single ad!');
129e41f4b71Sopenharmony_ci  load.loadAd(adRequestParam, adOptions, adLoaderListener);
130e41f4b71Sopenharmony_ci}
131e41f4b71Sopenharmony_ci```
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_ci### loadAdWithMultiSlots
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_ciloadAdWithMultiSlots(adParams: AdRequestParams[], adOptions: AdOptions, listener: MultiSlotsAdLoadListener): void
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ciLoads multiple ads.
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Advertising.Ads
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci**Parameters**
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 
147e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
148e41f4b71Sopenharmony_ci| adParams | [AdRequestParams](#adrequestparams)[] | Yes| Ad request parameters.| 
149e41f4b71Sopenharmony_ci| adOptions | [AdOptions](#adoptions) | Yes| Ad configuration.| 
150e41f4b71Sopenharmony_ci| listener | [MultiSlotsAdLoadListener](#multislotsadloadlistener) | Yes| Ad request callback.| 
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci**Error codes**
153e41f4b71Sopenharmony_ci
154e41f4b71Sopenharmony_ciFor details about the following error codes, see [Ads Service Framework Error Codes](errorcode-ads.md).
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ci| ID| Error Message| 
157e41f4b71Sopenharmony_ci| -------- | -------- |
158e41f4b71Sopenharmony_ci| 401      | Invalid input parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 
159e41f4b71Sopenharmony_ci| 801      | Device not supported.  |
160e41f4b71Sopenharmony_ci| 21800001 | System internal error. | 
161e41f4b71Sopenharmony_ci| 21800003 | Failed to load the ad request. | 
162e41f4b71Sopenharmony_ci
163e41f4b71Sopenharmony_ci**Example**
164e41f4b71Sopenharmony_ci
165e41f4b71Sopenharmony_ciFor details about how to obtain the context, see [Context](../../application-models/application-context-stage.md#overview).
166e41f4b71Sopenharmony_ci
167e41f4b71Sopenharmony_ci```ts
168e41f4b71Sopenharmony_ciimport { advertising } from '@kit.AdsKit';
169e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
170e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit';
171e41f4b71Sopenharmony_ci
172e41f4b71Sopenharmony_cifunction requestMultiAd(context: common.Context): void {
173e41f4b71Sopenharmony_ci  const adRequestParamArray: advertising.AdRequestParams[] = [{
174e41f4b71Sopenharmony_ci    // Ad type.
175e41f4b71Sopenharmony_ci    adType: 3,
176e41f4b71Sopenharmony_ci    // Ad ID.
177e41f4b71Sopenharmony_ci    adId: "testy63txaom86"
178e41f4b71Sopenharmony_ci  } as advertising.AdRequestParams,
179e41f4b71Sopenharmony_ci    {
180e41f4b71Sopenharmony_ci      // Ad type.
181e41f4b71Sopenharmony_ci      adType: 3,
182e41f4b71Sopenharmony_ci      // Ad ID.
183e41f4b71Sopenharmony_ci      adId: "testy63txaom86"
184e41f4b71Sopenharmony_ci    } as advertising.AdRequestParams
185e41f4b71Sopenharmony_ci  ];
186e41f4b71Sopenharmony_ci  const adOptions: advertising.AdOptions = {
187e41f4b71Sopenharmony_ci    // Optional custom parameter, which specifies whether the ad can be downloaded while mobile data is in use. The value 1 means that the ad can be downloaded while mobile data is in use, and 0 means the opposite.
188e41f4b71Sopenharmony_ci    allowMobileTraffic: 0,
189e41f4b71Sopenharmony_ci    // Set the maximum ad content rating. The following values are available: w: ages 3+, all audiences; PI: ages 7+, audiences under parental instruction; J: ages 12+, teenagers; A: ages 16+/18+, adults.
190e41f4b71Sopenharmony_ci    adContentClassification: 'A',
191e41f4b71Sopenharmony_ci    // Specify whether you want your ad content to be treated as COPPA-compliant. The following values are available: -1 (default value): uncertain; 0: no; 1: yes.
192e41f4b71Sopenharmony_ci    tagForChildProtection: -1,
193e41f4b71Sopenharmony_ci    // Specify whether you want the ad request to be processed in a way that meets the GDPR for users in the EEA under the age of consent. The following values are available: -1 (default value): uncertain; 0: no; 1: yes.
194e41f4b71Sopenharmony_ci    tagForUnderAgeOfPromise: -1
195e41f4b71Sopenharmony_ci  };
196e41f4b71Sopenharmony_ci  // Listener for the ad loading status.
197e41f4b71Sopenharmony_ci  const multiSlotsAdLoaderListener: advertising.MultiSlotsAdLoadListener = {
198e41f4b71Sopenharmony_ci    // Called when the ad request fails.
199e41f4b71Sopenharmony_ci    onAdLoadFailure: (errorCode: number, errorMsg: string) => {
200e41f4b71Sopenharmony_ci      hilog.error(0x0000, 'testTag', '%{public}s',
201e41f4b71Sopenharmony_ci        `request multi ads errorCode is: ${errorCode}, errorMsg is: ${errorMsg}`);
202e41f4b71Sopenharmony_ci    },
203e41f4b71Sopenharmony_ci    // Called when the ad request is successful.
204e41f4b71Sopenharmony_ci    onAdLoadSuccess: (ads: Map<string, Array<advertising.Advertisement>>) => {
205e41f4b71Sopenharmony_ci      hilog.info(0x0000, 'testTag', '%{public}s', 'succeed in requesting multi ads!');
206e41f4b71Sopenharmony_ci      // Save the requested ad content for display.
207e41f4b71Sopenharmony_ci      let returnAds: Array<advertising.Advertisement> = [];
208e41f4b71Sopenharmony_ci      ads.forEach((adsArray) => returnAds.push(...adsArray));
209e41f4b71Sopenharmony_ci    }
210e41f4b71Sopenharmony_ci  };
211e41f4b71Sopenharmony_ci  // Create an AdLoader object.
212e41f4b71Sopenharmony_ci  const load: advertising.AdLoader = new advertising.AdLoader(context);
213e41f4b71Sopenharmony_ci  // Load the ad.
214e41f4b71Sopenharmony_ci  hilog.info(0x0000, 'testTag', '%{public}s', 'request multi ads!');
215e41f4b71Sopenharmony_ci  load.loadAdWithMultiSlots(adRequestParamArray, adOptions, multiSlotsAdLoaderListener);
216e41f4b71Sopenharmony_ci}
217e41f4b71Sopenharmony_ci```
218e41f4b71Sopenharmony_ci
219e41f4b71Sopenharmony_ci
220e41f4b71Sopenharmony_ci## showAd
221e41f4b71Sopenharmony_ci
222e41f4b71Sopenharmony_cishowAd(ad: Advertisement, options: AdDisplayOptions, context?: common.UIAbilityContext): void
223e41f4b71Sopenharmony_ci
224e41f4b71Sopenharmony_ciShows a full-screen ad.
225e41f4b71Sopenharmony_ci
226e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
227e41f4b71Sopenharmony_ci
228e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Advertising.Ads
229e41f4b71Sopenharmony_ci
230e41f4b71Sopenharmony_ci**Parameters**
231e41f4b71Sopenharmony_ci
232e41f4b71Sopenharmony_ci
233e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 
234e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
235e41f4b71Sopenharmony_ci| ad | [Advertisement](#advertisement) | Yes| Ad object.| 
236e41f4b71Sopenharmony_ci| options | [AdDisplayOptions](#addisplayoptions) | Yes| Ad display parameters.| 
237e41f4b71Sopenharmony_ci| context | common.[UIAbilityContext](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md) | No| Context of the UIAbility. If this parameter is not set, the value is obtained from @ohos.app.ability.common.| 
238e41f4b71Sopenharmony_ci
239e41f4b71Sopenharmony_ci
240e41f4b71Sopenharmony_ci**Error codes**
241e41f4b71Sopenharmony_ci
242e41f4b71Sopenharmony_ci
243e41f4b71Sopenharmony_ciFor details about the following error codes, see [Ads Service Framework Error Codes](errorcode-ads.md).
244e41f4b71Sopenharmony_ci
245e41f4b71Sopenharmony_ci
246e41f4b71Sopenharmony_ci| ID| Error Message| 
247e41f4b71Sopenharmony_ci| -------- | -------- |
248e41f4b71Sopenharmony_ci| 401 | Invalid input parameter. Possible causes: 1. Mandatory parameters are left unspecified.| 
249e41f4b71Sopenharmony_ci| 21800001 | System internal error. | 
250e41f4b71Sopenharmony_ci| 21800004 | Failed to display the ad. | 
251e41f4b71Sopenharmony_ci
252e41f4b71Sopenharmony_ci
253e41f4b71Sopenharmony_ci**Example**
254e41f4b71Sopenharmony_ci
255e41f4b71Sopenharmony_ci```ts
256e41f4b71Sopenharmony_ciimport { advertising } from '@kit.AdsKit';
257e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 
258e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
259e41f4b71Sopenharmony_ci
260e41f4b71Sopenharmony_ci@Entry
261e41f4b71Sopenharmony_ci@Component
262e41f4b71Sopenharmony_ciexport struct ShowAd {
263e41f4b71Sopenharmony_ci  private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
264e41f4b71Sopenharmony_ci  // Requested ad content.
265e41f4b71Sopenharmony_ci  private ad?: advertising.Advertisement;
266e41f4b71Sopenharmony_ci  // Ad display parameters.
267e41f4b71Sopenharmony_ci  private adDisplayOptions: advertising.AdDisplayOptions = {
268e41f4b71Sopenharmony_ci    // Whether to mute the ad. By default, the ad is not muted.
269e41f4b71Sopenharmony_ci    mute: false
270e41f4b71Sopenharmony_ci  }
271e41f4b71Sopenharmony_ci
272e41f4b71Sopenharmony_ci  build() {
273e41f4b71Sopenharmony_ci    Column() {
274e41f4b71Sopenharmony_ci        Button ('Show Ad')
275e41f4b71Sopenharmony_ci          .onClick(() => {
276e41f4b71Sopenharmony_ci            try {
277e41f4b71Sopenharmony_ci              // Show the ad.
278e41f4b71Sopenharmony_ci              advertising.showAd(this.ad, this.adDisplayOptions, this.context);
279e41f4b71Sopenharmony_ci            } catch (err) {
280e41f4b71Sopenharmony_ci              hilog.error(0x0000, 'testTag', '%{public}s', `show ad catch error: ${err.code} ${err.message}`);
281e41f4b71Sopenharmony_ci            }
282e41f4b71Sopenharmony_ci          });
283e41f4b71Sopenharmony_ci    }
284e41f4b71Sopenharmony_ci    .width('100%')
285e41f4b71Sopenharmony_ci    .height('100%')
286e41f4b71Sopenharmony_ci  }
287e41f4b71Sopenharmony_ci}
288e41f4b71Sopenharmony_ci```
289e41f4b71Sopenharmony_ci
290e41f4b71Sopenharmony_ci
291e41f4b71Sopenharmony_ci## AdOptions
292e41f4b71Sopenharmony_ci
293e41f4b71Sopenharmony_ciDefines the ad configuration.
294e41f4b71Sopenharmony_ci
295e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
296e41f4b71Sopenharmony_ci
297e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Advertising.Ads
298e41f4b71Sopenharmony_ci
299e41f4b71Sopenharmony_ci
300e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 
301e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
302e41f4b71Sopenharmony_ci| tagForChildProtection | number | No| Tag for child protection, which specifies whether you want the ad content to be treated as COPPA-compliant.<br>- **-1** (default value): uncertain. You do not want to specify whether the ad content needs to be treated as COPPA-compliant.<br>- **0**: No. You do not want the ad content to be treated as COPPA-compliant.<br>- 1: Yes. You want the ad content to be treated as COPPA-compliant. The default value is **-1**.| 
303e41f4b71Sopenharmony_ci| adContentClassification | string | No| Maximum ad content rating.<br>- **W**: ages 3+, all audiences.<br>-** PI**: ages 7+, audiences under parental instruction.<br>- **J**: ages 12+, teenagers.<br>- **A**: ages 16+/18+, adults. The default value is "".| 
304e41f4b71Sopenharmony_ci| nonPersonalizedAd | number | No| Whether to request only non-personalized ads.<br>- **0**: request for personalized and non-personalized ads.<br>- **1**: request for only non-personalized ads. If this parameter is left blank, the service logic prevails.| 
305e41f4b71Sopenharmony_ci| [key: string] | number \| boolean \| string \| undefined | No| Custom parameters.<br> - **totalDuration**: The value is of the number type, in seconds. This parameter is mandatory for roll ads and is used to set the total duration of roll ads.<br> - **placementAdCountDownDesc**: The value is of the string type. This parameter is optional for roll ads and is used to set the countdown description of roll ads. This parameter must be encoded using the **encodeURI()** API. If this parameter is set, the countdown description is displayed. Otherwise, only the countdown is displayed.<br> - **allowMobileTraffic**: The value is of the number type. This parameter is optional. It specifies whether ads can be downloaded while mobile data is in use. The value **1** means that ads can be downloaded while mobile data is in use, and **0** means the opposite.<br> - **tagForUnderAgeOfPromise**: The value is of the number type. This parameter is optional. It specifies whether you want the ad request to be processed in a way that meets the GDPR for users in the EEA under the age of consent. The value **-1** means that you do not specify whether the ad request should be processed in a way that meets the GDPR for users in the EEA under the age of consent. The value **0** means that you do not want the ad request to be processed in a way that meets the GDPR for users in the EEA under the age of consent, and **1** means the opposite.|
306e41f4b71Sopenharmony_ci
307e41f4b71Sopenharmony_ci
308e41f4b71Sopenharmony_ci## AdRequestParams
309e41f4b71Sopenharmony_ci
310e41f4b71Sopenharmony_ciDefines the ad request parameters.
311e41f4b71Sopenharmony_ci
312e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
313e41f4b71Sopenharmony_ci
314e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Advertising.Ads
315e41f4b71Sopenharmony_ci
316e41f4b71Sopenharmony_ci
317e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
318e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
319e41f4b71Sopenharmony_ci| adId | string | Yes| Ad ID| 
320e41f4b71Sopenharmony_ci| adType | number | No| Type of the requested ad.<br>- **1**: splash ad.<br>- **3**: native ad.<br>- **7**: rewarded ad.<br>- **8**: banner ad.<br>- **12**: interstitial ad.<br>- **60**: roll ad.|
321e41f4b71Sopenharmony_ci| adCount | number | No| Number of ads requested. If this parameter is left blank, the service logic prevails.| 
322e41f4b71Sopenharmony_ci| adWidth | number | No| Ad width. The value must be greater than 0. If this parameter is left blank, the service logic prevails.| 
323e41f4b71Sopenharmony_ci| adHeight | number | No| Ad height. The value must be greater than 0. If this parameter is left blank, the service logic prevails.| 
324e41f4b71Sopenharmony_ci| adSearchKeyword | string | No| Ad keyword. If this parameter is left blank, "" is used by default.| 
325e41f4b71Sopenharmony_ci| [key: string] | number \| boolean \| string \| undefined | No| Custom parameters.| 
326e41f4b71Sopenharmony_ci
327e41f4b71Sopenharmony_ci
328e41f4b71Sopenharmony_ci## AdLoadListener
329e41f4b71Sopenharmony_ci
330e41f4b71Sopenharmony_ciEnumerates the callbacks used for the request for loading an ad.
331e41f4b71Sopenharmony_ci
332e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
333e41f4b71Sopenharmony_ci
334e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Advertising.Ads
335e41f4b71Sopenharmony_ci
336e41f4b71Sopenharmony_ci### onAdLoadFailure
337e41f4b71Sopenharmony_ci
338e41f4b71Sopenharmony_cionAdLoadFailure(errorCode: number, errorMsg: string): void
339e41f4b71Sopenharmony_ci
340e41f4b71Sopenharmony_ciCalled when an ad request fails.
341e41f4b71Sopenharmony_ci
342e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
343e41f4b71Sopenharmony_ci
344e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Advertising.Ads
345e41f4b71Sopenharmony_ci
346e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 
347e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
348e41f4b71Sopenharmony_ci| errorCode | number | Yes| Result code indicating the ad request failure.| 
349e41f4b71Sopenharmony_ci| errorMsg | string | Yes| Error message about the ad request failure.| 
350e41f4b71Sopenharmony_ci
351e41f4b71Sopenharmony_ci
352e41f4b71Sopenharmony_ci### onAdLoadSuccess
353e41f4b71Sopenharmony_ci
354e41f4b71Sopenharmony_cionAdLoadSuccess(ads: Array&lt;advertising.Advertisement&gt;): void
355e41f4b71Sopenharmony_ci
356e41f4b71Sopenharmony_ciCalled when an ad request is successful.
357e41f4b71Sopenharmony_ci
358e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
359e41f4b71Sopenharmony_ci
360e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Advertising.Ads
361e41f4b71Sopenharmony_ci
362e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 
363e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
364e41f4b71Sopenharmony_ci| ads | Array&lt;advertising.[Advertisement](#advertisement)&gt; | Yes| Ad data.| 
365e41f4b71Sopenharmony_ci
366e41f4b71Sopenharmony_ci
367e41f4b71Sopenharmony_ci**Example**
368e41f4b71Sopenharmony_ci
369e41f4b71Sopenharmony_ci```ts
370e41f4b71Sopenharmony_ciimport { advertising } from '@kit.AdsKit';
371e41f4b71Sopenharmony_ci
372e41f4b71Sopenharmony_cilet adLoaderListener: advertising.AdLoadListener = {
373e41f4b71Sopenharmony_ci  onAdLoadFailure: (errorCode: number, errorMsg: string) => {
374e41f4b71Sopenharmony_ci  },
375e41f4b71Sopenharmony_ci  onAdLoadSuccess: (ads: Array<advertising.Advertisement>) => {
376e41f4b71Sopenharmony_ci  }
377e41f4b71Sopenharmony_ci}
378e41f4b71Sopenharmony_ci
379e41f4b71Sopenharmony_ci```
380e41f4b71Sopenharmony_ci
381e41f4b71Sopenharmony_ci
382e41f4b71Sopenharmony_ci## MultiSlotsAdLoadListener
383e41f4b71Sopenharmony_ci
384e41f4b71Sopenharmony_ciEnumerates the callbacks used for the request for loading multiple ads.
385e41f4b71Sopenharmony_ci
386e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
387e41f4b71Sopenharmony_ci
388e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Advertising.Ads
389e41f4b71Sopenharmony_ci
390e41f4b71Sopenharmony_ci### onAdLoadFailure
391e41f4b71Sopenharmony_ci
392e41f4b71Sopenharmony_cionAdLoadFailure(errorCode: number, errorMsg: string): void
393e41f4b71Sopenharmony_ci
394e41f4b71Sopenharmony_ciCalled when a request for loading multiple ads fails.
395e41f4b71Sopenharmony_ci
396e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
397e41f4b71Sopenharmony_ci
398e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Advertising.Ads
399e41f4b71Sopenharmony_ci
400e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 
401e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
402e41f4b71Sopenharmony_ci| errorCode | number | Yes| Result code indicating the ad request failure.| 
403e41f4b71Sopenharmony_ci| errorMsg | string | Yes| Error message about the ad request failure.| 
404e41f4b71Sopenharmony_ci
405e41f4b71Sopenharmony_ci
406e41f4b71Sopenharmony_ci### onAdLoadSuccess
407e41f4b71Sopenharmony_ci
408e41f4b71Sopenharmony_cionAdLoadSuccess(adsMap: Map&lt;string, Array&lt;advertising.Advertisement&gt;&gt;): void
409e41f4b71Sopenharmony_ci
410e41f4b71Sopenharmony_ciCalled when a request for loading multiple ads is successful.
411e41f4b71Sopenharmony_ci
412e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
413e41f4b71Sopenharmony_ci
414e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Advertising.Ads
415e41f4b71Sopenharmony_ci
416e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 
417e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
418e41f4b71Sopenharmony_ci| adsMap |  Map&lt;string, Array&lt;advertising.[Advertisement](#advertisement)&gt;&gt;| Yes| Ad data.| 
419e41f4b71Sopenharmony_ci
420e41f4b71Sopenharmony_ci
421e41f4b71Sopenharmony_ci**Example**
422e41f4b71Sopenharmony_ci
423e41f4b71Sopenharmony_ci```ts
424e41f4b71Sopenharmony_ciimport { advertising } from '@kit.AdsKit';
425e41f4b71Sopenharmony_ci
426e41f4b71Sopenharmony_cilet adLoaderListener: advertising.MultiSlotsAdLoadListener = {
427e41f4b71Sopenharmony_ci  onAdLoadFailure: (errorCode: number, errorMsg: string) => {
428e41f4b71Sopenharmony_ci  },
429e41f4b71Sopenharmony_ci  onAdLoadSuccess: (adsMap: Map<string, Array<advertising.Advertisement>>) => {
430e41f4b71Sopenharmony_ci  }
431e41f4b71Sopenharmony_ci}
432e41f4b71Sopenharmony_ci```
433e41f4b71Sopenharmony_ci
434e41f4b71Sopenharmony_ci
435e41f4b71Sopenharmony_ci## Advertisement
436e41f4b71Sopenharmony_ci
437e41f4b71Sopenharmony_ci
438e41f4b71Sopenharmony_ciDefines the requested ad content.
439e41f4b71Sopenharmony_ci
440e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
441e41f4b71Sopenharmony_ci
442e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Advertising.Ads
443e41f4b71Sopenharmony_ci
444e41f4b71Sopenharmony_ci
445e41f4b71Sopenharmony_ci| Name| Type| Read-Only| Mandatory| Description|
446e41f4b71Sopenharmony_ci| -------- | --------| -------- | -------- | -------- |
447e41f4b71Sopenharmony_ci| adType | number | No|Yes| Ad type.| 
448e41f4b71Sopenharmony_ci| uniqueId | string | No|Yes| Unique ID of the ad.| 
449e41f4b71Sopenharmony_ci| rewarded | boolean | No|Yes| Whether users get rewarded for watching or clicking the ad.<br>- **true**: Users get rewarded.<br>- **false**: Users do not get rewarded.| 
450e41f4b71Sopenharmony_ci| shown | boolean | No|Yes| Whether the ad is shown.<br>- **true**: The ad is shown.<br>- **false**: The ad is not shown.| 
451e41f4b71Sopenharmony_ci| clicked | boolean | No|Yes| Whether the ad is clicked.<br>- **true**: The ad is clicked.<br>- **false**: The ad is not clicked.| 
452e41f4b71Sopenharmony_ci| rewardVerifyConfig | Map&lt;string, string&gt; | No|Yes| Server verification parameter.<br>{<br>customData: "test",<br>userId: "12345"<br>} | 
453e41f4b71Sopenharmony_ci| [key: string] | Object | No|Yes| Custom parameters.<br>- **isFullScreen**: The value is of the Boolean type. This parameter is used for splash ads to specify whether such an ad is in full-screen mode. The value **true** means that the ad is in full-screen mode, and **false** means that the ad is in half-screen mode.|
454e41f4b71Sopenharmony_ci
455e41f4b71Sopenharmony_ci
456e41f4b71Sopenharmony_ci## AdDisplayOptions
457e41f4b71Sopenharmony_ci
458e41f4b71Sopenharmony_ci
459e41f4b71Sopenharmony_ciDefines the ad display parameters.
460e41f4b71Sopenharmony_ci
461e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
462e41f4b71Sopenharmony_ci
463e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Advertising.Ads
464e41f4b71Sopenharmony_ci
465e41f4b71Sopenharmony_ci
466e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
467e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
468e41f4b71Sopenharmony_ci| customData | string | No| Custom media data. It is used by the server to notify the media server that a user should be rewarded for interacting with the ad, so as to avoid spoofing. If this parameter is left blank, no notification is sent.| 
469e41f4b71Sopenharmony_ci| userId | string | No| User ID. It is used by the server to notify the media server that a user should be rewarded for interacting with the ad, so as to avoid spoofing. If this parameter is left blank, no notification is sent.| 
470e41f4b71Sopenharmony_ci| useMobileDataReminder | boolean | No| Whether to display a dialog box to notify users when they use mobile data to play videos or download applications.<br>- **true**: A dialog box is displayed.<br>- **false**: No dialog box is displayed. This parameter depends on the mobile data dialog box function. Currently, the complete function is not supported, and therefore the default value is not determined.| 
471e41f4b71Sopenharmony_ci| mute | boolean | No| Whether to mute the ad video.<br>- **true**: The ad video is muted.<br>- **false**: The ad video is not muted. If this parameter is left blank, the default value **true** is used.| 
472e41f4b71Sopenharmony_ci| audioFocusType | number | No| Type of the scenario where the audio focus is obtained during video playback.<br>- **0**: The focus is obtained when the video is played in mute or non-mute mode.<br>- **1**: The focus is not obtained when the video is played in mute mode.<br>- **2**: The focus is not obtained when the video is played in mute or non-mute mode. Currently, the function on which this API depends is not supported, and therefore the default value is not determined.| 
473e41f4b71Sopenharmony_ci| [key: string] | number \| boolean \| string \| undefined | No| Custom parameters.<br>- **refreshTime**: The value is of the number type, in ms. The value is in the range [30000, 120000]. This parameter is optional for the AutoAdComponent module and specifies the interval at which the ads rotate. If this parameter is set, ads are rotated at the interval specified by this parameter. Otherwise, ads are not rotated and only the first ad in the ad response is displayed.|
474e41f4b71Sopenharmony_ci
475e41f4b71Sopenharmony_ci
476e41f4b71Sopenharmony_ci## AdInteractionListener
477e41f4b71Sopenharmony_ci
478e41f4b71Sopenharmony_ci
479e41f4b71Sopenharmony_ciDefines the ad status change callback.
480e41f4b71Sopenharmony_ci
481e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
482e41f4b71Sopenharmony_ci
483e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Advertising.Ads
484e41f4b71Sopenharmony_ci
485e41f4b71Sopenharmony_ci### onStatusChanged
486e41f4b71Sopenharmony_ci
487e41f4b71Sopenharmony_cionStatusChanged(status: string, ad: advertising.[Advertisement](#advertisement), data: string)
488e41f4b71Sopenharmony_ci
489e41f4b71Sopenharmony_ciCalled when the ad display status changes.
490e41f4b71Sopenharmony_ci
491e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
492e41f4b71Sopenharmony_ci
493e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Advertising.Ads
494e41f4b71Sopenharmony_ci
495e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
496e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
497e41f4b71Sopenharmony_ci| status | string | Yes| **status**: ad display status, which can be<br>**onAdOpen**, **onAdClose**, **onAdClick**, **onVideoPlayBegin**, **onVideoPlayEnd**, **onAdLoad**, **onAdFail**, **onMediaProgress**, **onMediaStart**, **onMediaPause**, **onMediaStop**, **onMediaComplete**, **onMediaError**, **onLandscape**, **onPortrait**, **onAdReward**, **onMediaCountDown**, or **onBackClicked**.| 
498e41f4b71Sopenharmony_ci| ad | advertising.[Advertisement](#advertisement) | Yes| Content of the ad.| 
499e41f4b71Sopenharmony_ci| data | string | Yes| Extended information.| 
500e41f4b71Sopenharmony_ci
501e41f4b71Sopenharmony_ci**Example**
502e41f4b71Sopenharmony_ci
503e41f4b71Sopenharmony_ci```ts
504e41f4b71Sopenharmony_ciimport { advertising } from '@kit.AdsKit';
505e41f4b71Sopenharmony_ci
506e41f4b71Sopenharmony_cilet adInteractionListener: advertising.AdInteractionListener = {
507e41f4b71Sopenharmony_ci  onStatusChanged: (status: string, ad: advertising.Advertisement, data: string) => {
508e41f4b71Sopenharmony_ci
509e41f4b71Sopenharmony_ci  }
510e41f4b71Sopenharmony_ci}
511e41f4b71Sopenharmony_ci```
512e41f4b71Sopenharmony_ci
513e41f4b71Sopenharmony_ci## getAdRequestBody<sup>12+</sup>  
514e41f4b71Sopenharmony_cigetAdRequestBody(adParams: AdRequestParams[], adOptions: AdOptions): Promise&lt;string&gt;
515e41f4b71Sopenharmony_ci
516e41f4b71Sopenharmony_ciObtains the body of an ad request. This API uses a promise to return the result.
517e41f4b71Sopenharmony_ci
518e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Advertising.Ads
519e41f4b71Sopenharmony_ci
520e41f4b71Sopenharmony_ci**Parameters**
521e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 
522e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
523e41f4b71Sopenharmony_ci| adParams | [AdRequestParams[]](#adrequestparams) | Yes| Ad request parameters.| 
524e41f4b71Sopenharmony_ci| adOptions | [AdOptions](#adoptions) | Yes| Ad configuration.| 
525e41f4b71Sopenharmony_ci
526e41f4b71Sopenharmony_ci**Return value**
527e41f4b71Sopenharmony_ci
528e41f4b71Sopenharmony_ci| Type| Description| 
529e41f4b71Sopenharmony_ci| -------- | -------- |
530e41f4b71Sopenharmony_ci| Promise&lt;string&gt; | Promise used to return the ad data of the string type.| 
531e41f4b71Sopenharmony_ci
532e41f4b71Sopenharmony_ci**Error codes**
533e41f4b71Sopenharmony_ci
534e41f4b71Sopenharmony_ciFor details about the following error codes, see [Ads Service Framework Error Codes](errorcode-ads.md).
535e41f4b71Sopenharmony_ci
536e41f4b71Sopenharmony_ci| ID| Error Message                        |
537e41f4b71Sopenharmony_ci| -------- | -------------------------------- |
538e41f4b71Sopenharmony_ci| 401      | Invalid input parameter. Possible causes: 1.Mandatory parameters are left unspecified |
539e41f4b71Sopenharmony_ci| 801      | Device not supported.            |
540e41f4b71Sopenharmony_ci| 21800001 | System internal error.           |
541e41f4b71Sopenharmony_ci
542e41f4b71Sopenharmony_ci**Example** 
543e41f4b71Sopenharmony_ci
544e41f4b71Sopenharmony_ci```ts
545e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit';
546e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
547e41f4b71Sopenharmony_ciimport { advertising } from '@kit.AdsKit';
548e41f4b71Sopenharmony_ciimport { Prompt } from '@kit.ArkUI';
549e41f4b71Sopenharmony_ci
550e41f4b71Sopenharmony_cifunction getAdRequestBody(): void {
551e41f4b71Sopenharmony_ci  let adReqParamsListForRequest: Array<advertising.AdRequestParams> = [];
552e41f4b71Sopenharmony_ci  const adReqParams: Record<string, Object> = {
553e41f4b71Sopenharmony_ci    'adId': 'testu7m3hc4gvm',
554e41f4b71Sopenharmony_ci    'adType': 3,
555e41f4b71Sopenharmony_ci    'adCount': 2,
556e41f4b71Sopenharmony_ci    'adWidth': 100,
557e41f4b71Sopenharmony_ci    'adHeight': 100
558e41f4b71Sopenharmony_ci  };
559e41f4b71Sopenharmony_ci
560e41f4b71Sopenharmony_ci  adReqParamsListForRequest.push(adReqParams as advertising.AdRequestParams);
561e41f4b71Sopenharmony_ci  const adOption: Record<string, Object> = {
562e41f4b71Sopenharmony_ci    // Set the maximum ad content rating. The following values are available: w: ages 3+, all audiences; PI: ages 7+, audiences under parental instruction; J: ages 12+, teenagers; A: ages 16+/18+, adults.
563e41f4b71Sopenharmony_ci    'adContentClassification': 'A',
564e41f4b71Sopenharmony_ci    // Set whether to request only non-personalized ads. 0: to request personalized ads and non-personalized ads; 1: to request only non-personalized ads. If this parameter is left blank, the service logic prevails.
565e41f4b71Sopenharmony_ci    'nonPersonalizedAd': 0,
566e41f4b71Sopenharmony_ci    // Specify whether you want your ad content to be treated as COPPA-compliant. The following values are available: -1 (default value): uncertain; 0: no; 1: yes.
567e41f4b71Sopenharmony_ci    'tagForChildProtection': 1,
568e41f4b71Sopenharmony_ci    // Specify whether you want the ad request to be processed in a way that meets the GDPR for users in the EEA under the age of consent. The following values are available: -1 (default value): uncertain; 0: no; 1: yes.
569e41f4b71Sopenharmony_ci    'tagForUnderAgeOfPromise': -1
570e41f4b71Sopenharmony_ci  };
571e41f4b71Sopenharmony_ci  advertising.getAdRequestBody(adReqParamsListForRequest, adOption as advertising.AdOptions).then((data) => {
572e41f4b71Sopenharmony_ci    hilog.info(0x0000, 'testTag', '%{public}s', `succeeded in getting AdRequestBody by promise: ${data}`);
573e41f4b71Sopenharmony_ci    Prompt.showToast({
574e41f4b71Sopenharmony_ci      message: data,
575e41f4b71Sopenharmony_ci      duration: 1000
576e41f4b71Sopenharmony_ci    });
577e41f4b71Sopenharmony_ci  }).catch((error: BusinessError) => {
578e41f4b71Sopenharmony_ci    hilog.error(0x0000, 'testTag', '%{public}s',
579e41f4b71Sopenharmony_ci      `getAdRequestBody failed, code: ${error.code}, message: ${error.message}`);
580e41f4b71Sopenharmony_ci    Prompt.showToast({
581e41f4b71Sopenharmony_ci      message: error.code.toString() + ',' + error.message,
582e41f4b71Sopenharmony_ci      duration: 1000
583e41f4b71Sopenharmony_ci    });
584e41f4b71Sopenharmony_ci  })
585e41f4b71Sopenharmony_ci}
586e41f4b71Sopenharmony_ci```
587e41f4b71Sopenharmony_ci
588e41f4b71Sopenharmony_ci## parseAdResponse<sup>12+</sup>
589e41f4b71Sopenharmony_ci
590e41f4b71Sopenharmony_ciparseAdResponse(adResponse: string, listener: MultiSlotsAdLoadListener, context: common.UIAbilityContext): void
591e41f4b71Sopenharmony_ci
592e41f4b71Sopenharmony_ciParses the body of an ad response.
593e41f4b71Sopenharmony_ci
594e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Advertising.Ads
595e41f4b71Sopenharmony_ci
596e41f4b71Sopenharmony_ci**Parameters**
597e41f4b71Sopenharmony_ci
598e41f4b71Sopenharmony_ci| Name| Type                                                    | Mandatory| Description                   |
599e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | ---- | ----------------------- |
600e41f4b71Sopenharmony_ci| adResponse | string                                                       | Yes  | Ad request parameters.         |
601e41f4b71Sopenharmony_ci| listener   | [MultiSlotsAdLoadListener](#multislotsadloadlistener)        | Yes  | Ad request callback.     |
602e41f4b71Sopenharmony_ci| context    | common.[UIAbilityContext](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md) | Yes  | UIAbility context.|
603e41f4b71Sopenharmony_ci
604e41f4b71Sopenharmony_ci**Error codes**
605e41f4b71Sopenharmony_ci
606e41f4b71Sopenharmony_ciFor details about the following error codes, see [Ads Service Framework Error Codes](errorcode-ads.md).
607e41f4b71Sopenharmony_ci
608e41f4b71Sopenharmony_ci| ID| Error Message                        |
609e41f4b71Sopenharmony_ci| -------- | -------------------------------- |
610e41f4b71Sopenharmony_ci| 401      | Invalid input parameter. Possible causes: 1.Mandatory parameters are left unspecified |
611e41f4b71Sopenharmony_ci| 801      | Device not supported.            |
612e41f4b71Sopenharmony_ci| 21800001 | System internal error.           |
613e41f4b71Sopenharmony_ci| 21800005 | Failed to parse the ad response. |
614e41f4b71Sopenharmony_ci
615e41f4b71Sopenharmony_ci**Example**
616e41f4b71Sopenharmony_ci
617e41f4b71Sopenharmony_ciFor details about how to obtain the context, see [Obtaining the Context of UIAbility](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md).
618e41f4b71Sopenharmony_ci
619e41f4b71Sopenharmony_ci```ts
620e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
621e41f4b71Sopenharmony_ciimport { advertising } from '@kit.AdsKit';
622e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit';
623e41f4b71Sopenharmony_ci
624e41f4b71Sopenharmony_cifunction parseAdResponse(adResponse: string, context: common.UIAbilityContext): void {
625e41f4b71Sopenharmony_ci  // Listen for the ad parsing callback.
626e41f4b71Sopenharmony_ci  const multiSlotsAdLoaderListener: advertising.MultiSlotsAdLoadListener = {
627e41f4b71Sopenharmony_ci    // Called when ad parsing fails.
628e41f4b71Sopenharmony_ci    onAdLoadFailure: (errorCode: number, errorMsg: string) => {
629e41f4b71Sopenharmony_ci      hilog.error(0x0000, 'testTag', '%{public}s',
630e41f4b71Sopenharmony_ci        `request multi ads errorCode is: ${errorCode}, errorMsg is: ${errorMsg}`);
631e41f4b71Sopenharmony_ci    },
632e41f4b71Sopenharmony_ci    // Called when ad parsing is successful.
633e41f4b71Sopenharmony_ci    onAdLoadSuccess: (ads: Map<string, Array<advertising.Advertisement>>) => {
634e41f4b71Sopenharmony_ci      hilog.info(0x0000, 'testTag', '%{public}s', 'succeeded in requesting multi ads!');
635e41f4b71Sopenharmony_ci      // Save the parsed ad content as an array for display.
636e41f4b71Sopenharmony_ci      let returnAds: Array<advertising.Advertisement> = [];
637e41f4b71Sopenharmony_ci      ads.forEach((adsArray) => returnAds.push(...adsArray));
638e41f4b71Sopenharmony_ci    }
639e41f4b71Sopenharmony_ci  };
640e41f4b71Sopenharmony_ci  // Call the API to parse the response body.
641e41f4b71Sopenharmony_ci  hilog.info(0x0000, 'testTag', '%{public}s', 'parse ad response!');
642e41f4b71Sopenharmony_ci  advertising.parseAdResponse(adResponse, multiSlotsAdLoaderListener, context);
643e41f4b71Sopenharmony_ci}
644e41f4b71Sopenharmony_ci```
645e41f4b71Sopenharmony_ci
646e41f4b71Sopenharmony_ci## registerWebAdInterface<sup>12+</sup>
647e41f4b71Sopenharmony_ci
648e41f4b71Sopenharmony_ciregisterWebAdInterface(controller: web_webview.WebviewController, context: common.UIAbilityContext): void
649e41f4b71Sopenharmony_ci
650e41f4b71Sopenharmony_ciInjects an ad JavaScript object to the **\<Web>** component.
651e41f4b71Sopenharmony_ci
652e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
653e41f4b71Sopenharmony_ci
654e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Advertising.Ads
655e41f4b71Sopenharmony_ci
656e41f4b71Sopenharmony_ci**Parameters**
657e41f4b71Sopenharmony_ci
658e41f4b71Sopenharmony_ci
659e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 
660e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
661e41f4b71Sopenharmony_ci| controller | web_webview.[WebviewController](../apis-arkweb/js-apis-webview.md#webviewcontroller) | Yes| Controller of the **\<Web>** component.| 
662e41f4b71Sopenharmony_ci| context | common.[UIAbilityContext](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md) | Yes| UIAbility context.| 
663e41f4b71Sopenharmony_ci
664e41f4b71Sopenharmony_ci
665e41f4b71Sopenharmony_ci**Error codes**
666e41f4b71Sopenharmony_ci
667e41f4b71Sopenharmony_ci
668e41f4b71Sopenharmony_ciFor details about the following error codes, see [Ads Service Framework Error Codes](errorcode-ads.md).
669e41f4b71Sopenharmony_ci
670e41f4b71Sopenharmony_ci
671e41f4b71Sopenharmony_ci| ID| Error Message| 
672e41f4b71Sopenharmony_ci| -------- | -------- |
673e41f4b71Sopenharmony_ci| 401 | Invalid input parameter. Possible causes: 1.Mandatory parameters are left unspecified | 
674e41f4b71Sopenharmony_ci| 21800001 | System internal error. | 
675e41f4b71Sopenharmony_ci
676e41f4b71Sopenharmony_ci
677e41f4b71Sopenharmony_ci**Example**
678e41f4b71Sopenharmony_ci
679e41f4b71Sopenharmony_ci```ts
680e41f4b71Sopenharmony_ciimport { webview } from '@kit.ArkWeb';
681e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
682e41f4b71Sopenharmony_ciimport { advertising } from '@kit.AdsKit';
683e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit';
684e41f4b71Sopenharmony_ci
685e41f4b71Sopenharmony_ci@Entry
686e41f4b71Sopenharmony_ci@Component
687e41f4b71Sopenharmony_cistruct Index {
688e41f4b71Sopenharmony_ci  private webController: webview.WebviewController = new webview.WebviewController();
689e41f4b71Sopenharmony_ci  private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
690e41f4b71Sopenharmony_ci
691e41f4b71Sopenharmony_ci  build() {
692e41f4b71Sopenharmony_ci    Column() {
693e41f4b71Sopenharmony_ci      Button('Inject Ad Object to Web')
694e41f4b71Sopenharmony_ci        .onClick(() => {
695e41f4b71Sopenharmony_ci          try {
696e41f4b71Sopenharmony_ci            advertising.registerWebAdInterface(this.webController, this.context);
697e41f4b71Sopenharmony_ci          } catch (err) {
698e41f4b71Sopenharmony_ci            hilog.error(0x0000, 'testTag', '%{public}s', 
699e41f4b71Sopenharmony_ci              `register web ad interface error: ${err.code}, ${err.message}`);
700e41f4b71Sopenharmony_ci          }
701e41f4b71Sopenharmony_ci        })
702e41f4b71Sopenharmony_ci
703e41f4b71Sopenharmony_ci      Web({
704e41f4b71Sopenharmony_ci        src: 'www.example.com',
705e41f4b71Sopenharmony_ci        controller: this.webController,
706e41f4b71Sopenharmony_ci      })
707e41f4b71Sopenharmony_ci        .width("100%")
708e41f4b71Sopenharmony_ci        .height("100%")
709e41f4b71Sopenharmony_ci    }
710e41f4b71Sopenharmony_ci  }
711e41f4b71Sopenharmony_ci}
712e41f4b71Sopenharmony_ci```
713