跳转至正文

快速入门

GitHub

你可以使用我们的 AI 辅助设置来安装插件。使用以下命令将 Capgo skills 添加到你的 AI 工具中:

终端窗口
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

然后使用以下 prompt:

Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-admob` plugin in my project.

如果你更倾向于手动设置,请运行以下命令安装插件,并参考下文针对各平台的具体说明:

终端窗口
bun add @capgo/capacitor-admob
npx cap sync
import { AdMob } from '@capgo/capacitor-admob';
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, MaxAdContentRating } from '@capgo/capacitor-admob';
await AdMob.configRequest({
maxAdContentRating: MaxAdContentRating.PG,
tagForChildDirectedTreatment: true,
testDeviceIds: ['test-device-id']
});
  • 监听器: 在页面卸载时移除句柄。
  • 仅需初始化一次: 在应用启动阶段运行 setup。
  • 单一事实来源: 信任 src/definitions.ts 在 GitHub 仓库中。

初始化并启动 AdMob SDK。

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

配置 AdMob 设置。

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

配置广告请求设置。

import { AdMob, MaxAdContentRating } 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 });

trackingAuthorizationStatus

“trackingAuthorizationStatus” 章节

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

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

requestTrackingAuthorization

“requestTrackingAuthorization” 章节

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

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

AdMob 配置选项。

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

广告请求配置。

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[];
};

移动广告基础选项。

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 发生变化时,请重新运行 sync。

如果你正在使用 快速入门 来规划 dashboard 和 API 操作,请将其连接至 使用 @capgo/capacitor-admob 在“使用 @capgo/capacitor-admob”中实现原生能力, API 概览 关于 API 概览中的实现细节, 简介 有关 Introduction 中的实现细节, API 密钥 有关 API Keys 中的实现细节,以及 设备 有关 Devices 中的实现细节。