跳过内容

开始使用

安装

安装
终端窗口
bun add @capgo/capacitor-admob
bunx cap sync

导入

导入
import { AdMob } from '@capgo/capacitor-admob';

Initialize and start the AdMob SDK.

import { AdMob } from '@capgo/capacitor-admob';
await AdMob.start();

启动

import { AdMob } from '@capgo/capacitor-admob';
await AdMob.configure({
appMuted: false,
appVolume: 0.5
});

配置广告请求设置。

import { AdMob } from '@capgo/capacitor-admob';
await AdMob.configRequest({
maxAdContentRating: MaxAdContentRating.PG,
tagForChildDirectedTreatment: true,
testDeviceIds: ['test-device-id']
});

创建一个新广告实例。

import { AdMob } from '@capgo/capacitor-admob';
await AdMob.adCreate({
adUnitId: 'ca-app-pub-3940256099942544/1033173712'
});

检查是否有广告已加载并准备显示。

import { AdMob } from '@capgo/capacitor-admob';
const isLoaded = await AdMob.adIsLoaded({ id: 1 });
if (isLoaded) {
await AdMob.adShow({ id: 1 });
}

加载广告。

import { AdMob } from '@capgo/capacitor-admob';
await AdMob.adLoad({ id: 1 });

显示已加载的广告。

import { AdMob } from '@capgo/capacitor-admob';
await AdMob.adShow({ id: 1 });

隐藏当前显示的广告。

import { AdMob } from '@capgo/capacitor-admob';
await AdMob.adHide({ id: 1 });

获取当前跟踪授权状态(仅限 iOS)。

import { AdMob } from '@capgo/capacitor-admob';
const { status } = await AdMob.trackingAuthorizationStatus();
if (status === TrackingAuthorizationStatus.notDetermined) {
await AdMob.requestTrackingAuthorization();
}

从用户请求跟踪授权(仅限 iOS)。

import { AdMob } from '@capgo/capacitor-admob';
const { status } = await AdMob.requestTrackingAuthorization();
console.log('User tracking status:', status);

类型参考

类型参考

AdMobConfig

AdMob配置

AdMob配置选项

export type AdMobConfig = {
/** Whether the app should be muted */
appMuted?: boolean;
/** The app volume (0.0 to 1.0) */
appVolume?: number;
};

RequestConfig

请求配置

广告请求配置

export type RequestConfig = {
/** Maximum ad content rating */
maxAdContentRating?: MaxAdContentRating;
/** Whether to use the same app key */
sameAppKey?: boolean;
/** Tag for child-directed treatment (true, false, or null for unspecified) */
tagForChildDirectedTreatment?: boolean | null;
/** Tag for under age of consent (true, false, or null for unspecified) */
tagForUnderAgeOfConsent?: boolean | null;
/** Array of test device IDs */
testDeviceIds?: string[];
};

MobileAdOptions

移动广告选项

移动广告基础选项

export type MobileAdOptions = {
/** The ad unit ID from AdMob */
adUnitId: string;
};

iOS App Tracking Transparency 的授权状态

export enum TrackingAuthorizationStatus {
/** User has not yet received an authorization request */
notDetermined = 0,
/** User restricted, device is unable to provide authorization */
restricted = 1,
/** User denied authorization */
denied = 2,
/** User authorized access */
authorized = 3,
}

用于限制广告内容等级的最大广告内容等级枚举

export enum MaxAdContentRating {
/** General Audiences */
G = 'G',
/** Mature Audiences */
MA = 'MA',
/** Parental Guidance */
PG = 'PG',
/** Teen */
T = 'T',
/** Unspecified rating */
UNSPECIFIED = '',
}

本页面由插件生成 src/definitions.ts当公共 API 上游发生变化时,请重新同步