整合廣告適用的 WebView API
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
WebView API for Ads 可讓您使用WebViewController
在應用程式中放送廣告並營利。如果您透過 WebViewController
在應用程式中顯示導入廣告的網頁內容 (使用 AdSense 程式碼或 Google 發布商代碼),請使用這個 API 啟用廣告營利功能。詳情請參閱 AdMob 政策。

- 使用 Google Mobile Ads SDK 發出廣告請求,藉此營利
您可以透過 Google Mobile Ads SDK 向 AdMob 發出廣告請求,並導入行動應用程式適用的廣告格式,藉此透過應用程式營利。
瞭解詳情。
- 使用 WebView API for Ads 營利
如果您的應用程式使用 WebViewController
顯示來自 Ad Manager 或 AdSense 的廣告,請使用 WebView API for Ads 向 Google Mobile Ads SDK 註冊 WebViewController
物件。AdSense 程式碼或 Google 發布商廣告代碼中的 JavaScript 會建立並傳送廣告請求,因此您不需要使用 SDK 提出任何廣告請求。請注意,這個 API 僅適用於行動版和電腦版網站的廣告空間格式。
如果您不擁有 WebViewController
中的網頁內容,我們仍建議您使用這個 API,協助保護廣告主免受垃圾內容干擾,並提升提供內容的網站發布商營利。
請注意,您可以在同一應用程式中,擇一或同時啟用兩個選項。
本指南旨在協助您在 iOS 應用程式中整合 WebView API for Ads。
事前準備
開始使用廣告的 WebView API 前,請務必完成下列事項:
略過應用程式 ID 檢查
Android
在 AndroidManifest.xml
檔案中新增下列 <meta-data>
標記,可略過 APPLICATION_ID
檢查。如果遺漏這個步驟,Google Mobile Ads SDK 可能會在應用程式啟動時擲回 IllegalStateException
。
<!-- Bypass APPLICATION_ID check for WebView API for Ads -->
<meta-data
android:name="com.google.android.gms.ads.INTEGRATION_MANAGER"
android:value="webview"/>
iOS
使用下列鍵和字串值更新 Runner/Info.plist
檔案,即可略過 GADApplicationIdentifier
檢查。如果遺漏這個步驟,Google Mobile Ads SDK 可能會在應用程式啟動時擲回 GADInvalidInitializationException
。
<!-- Bypass GADApplicationIdentifier check for WebView API for Ads -->
<key>GADIntegrationManager</key>
<string>webview</string>
註冊 WebViewController
如要改善使用 AdSense 程式碼或 Google 發布商代碼的 WebViewController
應用程式內廣告營利,請按照下列步驟操作:
在 WebViewController
中啟用 JavaScript。否則廣告可能無法載入。
為提升使用者廣告體驗,並與 Chrome 的Cookie 政策保持一致,請在 AndroidWebViewController
執行個體上啟用第三方 Cookie。
呼叫 Google Mobile Ads SDK 提供的 registerWebView()
方法,註冊 WebViewController
例項。
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:webview_flutter_android/webview_flutter_android.dart';
@override
class WebViewExampleState extends State<WebViewExample> {
late final WebViewController controller;
@override
void initState() {
super.initState();
createWebView();
}
void createWebView() async {
controller = WebViewController();
// 1. Enable JavaScript in the web view.
await controller.setJavaScriptMode(JavaScriptMode.unrestricted);
// 2. Enable third-party cookies for Android.
if (controller.platform is AndroidWebViewController) {
AndroidWebViewCookieManager cookieManager = AndroidWebViewCookieManager(
const PlatformWebViewCookieManagerCreationParams());
await cookieManager.setAcceptThirdPartyCookies(
controller.platform as AndroidWebViewController, true);
}
// 3. Register the web view.
await MobileAds.instance.registerWebView(controller);
}
}
載入網址
您現在可以透過 WebViewController
載入網址並顯示網頁內容。
建議您先載入這個測試網址:https://google.github.io/webview-ads/test/
,測試整合結果,再使用自己的網址。如果未啟用 JavaScript,網頁會顯示錯誤。
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:webview_flutter_android/webview_flutter_android.dart';
@override
class WebViewExampleState extends State<WebViewExample> {
late final WebViewController controller;
@override
void initState() {
super.initState();
createWebView();
}
void createWebView() async {
controller = WebViewController();
// 1. Enable JavaScript in the web view.
await controller.setJavaScriptMode(JavaScriptMode.unrestricted);
// 2. Enable third-party cookies for Android.
if (controller.platform is AndroidWebViewController) {
AndroidWebViewCookieManager cookieManager = AndroidWebViewCookieManager(
const PlatformWebViewCookieManagerCreationParams());
await cookieManager.setAcceptThirdPartyCookies(
controller.platform as AndroidWebViewController, true);
}
// 3. Register the web view.
await MobileAds.instance.registerWebView(controller);
// 4. Load the URL.
await controller.loadRequest(Uri.parse('https://google.github.io/webview-ads/test/'));
}
只要符合下列條件,測試網址就會顯示綠色狀態列,代表整合成功:
WebView
已連結至 Google Mobile Ads SDK
- 已啟用 JavaScript
- 第三方 Cookie 可正常運作 (iOS 裝置不應出現此情況)
- 第一方 Cookie 的運作方式
查看測試網址的原始碼。然後將測試網址換成您的網址。您也可以使用 Charles 等 Proxy 工具擷取應用程式的 HTTPS 流量,並檢查廣告請求的 &scar=
參數。

除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-09-02 (世界標準時間)。
[null,null,["上次更新時間:2025-09-02 (世界標準時間)。"],[[["\u003cp\u003eThe WebView API for Ads enables in-app ad monetization when displaying web content with AdSense or Google Publisher Tag ads through \u003ccode\u003eWebViewController\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eTo use this API, register your \u003ccode\u003eWebViewController\u003c/code\u003e objects with the Google Mobile Ads SDK, ensuring JavaScript and third-party cookies (for Android) are enabled.\u003c/p\u003e\n"],["\u003cp\u003eThis API simplifies ad serving by handling ad requests within the web content's JavaScript, eliminating the need for manual ad requests from your app.\u003c/p\u003e\n"],["\u003cp\u003eIt is recommended to use this API even for third-party web content to enhance ad monetization and protect against spam.\u003c/p\u003e\n"],["\u003cp\u003eBefore starting, ensure you're using the Google Mobile Ads SDK for Flutter plugin (v3.0.0+), \u003ccode\u003ewebview_flutter\u003c/code\u003e, and \u003ccode\u003ewebview_flutter_android\u003c/code\u003e (v3.7.0+) in your app.\u003c/p\u003e\n"]]],["The WebView API for Ads facilitates in-app ad monetization through `WebViewController`. To use this, register `WebViewController` objects with the Google Mobile Ads SDK. Enable JavaScript and third-party cookies in the `WebViewController` and then call `registerWebView()`. For Android, add a specific `\u003cmeta-data\u003e` tag in `AndroidManifest.xml`, and for iOS, update `Info.plist` to bypass checks. You can load web content that implements ads from AdSense or Google Publisher Tag via this API and use a test URL to verify integration.\n"],null,["The WebView API for Ads allows in-app ad monetization using\n[`WebViewController`](//pub.dev/documentation/webview_flutter/latest/webview_flutter/WebViewController-class.html).\nIf you display web content that implements ads with\n[AdSense code](//support.google.com/adsense/answer/9274634) or\n[Google Publisher Tag](//support.google.com/admanager/answer/181073)\nin your app through `WebViewController`, you should use this API to enable ads\nmonetization. To learn more, see the\n\n[AdMob policies](//support.google.com/admob/answer/48182#trs).\n\n\n1.\n\n Monetize by making ad requests with Google Mobile Ads SDK\n\n : You can monetize your app by making ad requests to\n\n AdMob with\n Google Mobile Ads SDK by implementing [ad formats for mobile\n app](//support.google.com/admob/answer/6128738).\n\n\n [Learn more](/admob/flutter/quick-start).\n2.\n\n Monetize by using the WebView API for Ads\n\n : If your app uses `WebViewController` to display web content that serves ads from\n [Ad Manager](//support.google.com/admanager) or\n [AdSense](//support.google.com/adsense), use the WebView API for Ads to register\n `WebViewController` objects with Google Mobile Ads SDK. The\n JavaScript in the [AdSense code](//support.google.com/adsense/answer/9274634)\n or [Google Publisher Tag](//support.google.com/admanager/answer/181073)\n builds and sends ad requests so you don't need to make any ad requests with\n the SDK. Keep in mind that only the mobile web and desktop web\n [inventory formats](//support.google.com/admanager/answer/9796545)\n are available using this API.\n\n If you don't own the web content in a `WebViewController`, you are still\n encouraged to use this API to help protect advertisers from spam and\n improve monetization for the web publishers that provided the content.\n\nNote that you can do either option, or even both, in the same app.\n\nThis guide is intended to help you integrate the WebView API for Ads into your\niOS app.\n\nBefore you begin\n\nBefore you start using the WebView API for Ads, make sure you do the following:\n\n- Use [Google Mobile Ads SDK for Flutter plugin](/admob/flutter/quick-start#import_the_mobile_ads_sdk) with version 3.0.0 or higher in your app.\n- Add [`webview_flutter`](//pub.dev/packages/webview_flutter) as a dependency in your `pubspec.yaml` file.\n- Add [`webview_flutter_android`](//pub.dev/packages/webview_flutter_android) with version 3.7.0 or higher in your app.\n\nBypass the check for application identifier \n\nAndroid\n\nAdd the following `\u003cmeta-data\u003e` tag in your `AndroidManifest.xml` file to\nbypass the check for the `APPLICATION_ID`. If you miss this step,\nGoogle Mobile Ads SDK might throw an\n[`IllegalStateException`](//developer.android.com/reference/java/lang/IllegalStateException)\non app start. \n\n \u003c!-- Bypass APPLICATION_ID check for WebView API for Ads --\u003e\n \u003cmeta-data\n android:name=\"com.google.android.gms.ads.INTEGRATION_MANAGER\"\n android:value=\"webview\"/\u003e\n\niOS\n\nUpdate the `Runner/Info.plist` file with the key and string value below to\nbypass a check for the `GADApplicationIdentifier`. If you miss this step,\nGoogle Mobile Ads SDK might throw a `GADInvalidInitializationException`\non app start. \n\n \u003c!-- Bypass GADApplicationIdentifier check for WebView API for Ads --\u003e\n \u003ckey\u003eGADIntegrationManager\u003c/key\u003e\n \u003cstring\u003ewebview\u003c/string\u003e\n\nRegister the WebViewController\n\nTo improve in-app ad monetization of ads within a\n`WebViewController` that uses [AdSense\ncode](//support.google.com/adsense/answer/9274634) or [Google Publisher\nTags](//support.google.com/admanager/answer/181073), follow the steps\nlisted below:\n\n1. Enable JavaScript in the `WebViewController`. Failure to do so can cause\n ads not to load.\n\n2. To improve your users' ad experience and be consistent with Chrome's\n [cookie policy](//policies.google.com/technologies/cookies), enable\n third-party cookies on your `AndroidWebViewController` instance.\n\n3. Register the `WebViewController` instance by calling the\n [`registerWebView()`](//pub.dev/documentation/google_mobile_ads/latest/google_mobile_ads/MobileAds/registerWebView.html)\n method provided by Google Mobile Ads SDK.\n\n import 'package:google_mobile_ads/google_mobile_ads.dart';\n import 'package:webview_flutter/webview_flutter.dart';\n import 'package:webview_flutter_android/webview_flutter_android.dart';\n\n @override\n class WebViewExampleState extends State\u003cWebViewExample\u003e {\n late final WebViewController controller;\n\n @override\n void initState() {\n super.initState();\n\n createWebView();\n }\n\n void createWebView() async {\n controller = WebViewController();\n // 1. Enable JavaScript in the web view.\n await controller.setJavaScriptMode(JavaScriptMode.unrestricted);\n // 2. Enable third-party cookies for Android.\n if (controller.platform is AndroidWebViewController) {\n AndroidWebViewCookieManager cookieManager = AndroidWebViewCookieManager(\n const PlatformWebViewCookieManagerCreationParams());\n await cookieManager.setAcceptThirdPartyCookies(\n controller.platform as AndroidWebViewController, true);\n }\n // 3. Register the web view.\n await MobileAds.instance.registerWebView(controller);\n }\n }\n\nLoad the URL\n\nYou can now load a URL and display your web content through `WebViewController`.\nWe recommend that you load this test URL:\n`https://google.github.io/webview-ads/test/` to test the integration prior to\nusing your own URL. The web page will show an error if JavaScript is not\nenabled. \n\n import 'package:google_mobile_ads/google_mobile_ads.dart';\n import 'package:webview_flutter/webview_flutter.dart';\n import 'package:webview_flutter_android/webview_flutter_android.dart';\n\n @override\n class WebViewExampleState extends State\u003cWebViewExample\u003e {\n late final WebViewController controller;\n\n @override\n void initState() {\n super.initState();\n\n createWebView();\n }\n\n void createWebView() async {\n controller = WebViewController();\n // 1. Enable JavaScript in the web view.\n await controller.setJavaScriptMode(JavaScriptMode.unrestricted);\n\n // 2. Enable third-party cookies for Android.\n if (controller.platform is AndroidWebViewController) {\n AndroidWebViewCookieManager cookieManager = AndroidWebViewCookieManager(\n const PlatformWebViewCookieManagerCreationParams());\n await cookieManager.setAcceptThirdPartyCookies(\n controller.platform as AndroidWebViewController, true);\n }\n\n // 3. Register the web view.\n await MobileAds.instance.registerWebView(controller);\n\n // 4. Load the URL.\n await controller.loadRequest(Uri.parse('https://google.github.io/webview-ads/test/'));\n }\n\nThe test URL shows green status bars for a successful integration if the\nfollowing conditions apply:\n\n- `WebView` connected to the Google Mobile Ads SDK\n- JavaScript enabled\n- Third-party cookies work (not expected on iOS devices)\n- First-party cookies work\n\nView the [source code](//github.com/google/webview-ads/blob/main/test/index.html)\nof our test URL. You can then replace the test URL with your URL. You can also\nuse a proxy tool such as [Charles](//www.charlesproxy.com/) to capture your\napp's HTTPS traffic and inspect the ad requests for a `&scar=` parameter."]]