廣告專用的 WebView API 可讓應用程式信號提供給 WKWebView
中的代碼,協助提供內容的發布商改善營利狀況,並保護廣告客戶免受垃圾訊息干擾。這些應用程式信號 (例如應用程式 ID 和應用程式版本) 有助於啟用報表和指定應用程式內瀏覽器廣告空間用途,而這類用途原本只適用於應用程式流量。
運作方式
與 Google Mobile Ads SDK 的通訊僅會在下列任一事件觸發時發生:
SDK 會將訊息處理常式新增至已註冊的 WKWebView
,以便監聽這些廣告事件。如要進一步瞭解運作方式,請查看測試頁面的原始碼。
必要條件
- Google Mobile Ads SDK 9.6.0 以上版本。
請使用下列鍵和字串值更新
Info.plist
檔案。這會略過 Google Mobile Ads SDK 對GADApplicationIdentifier
值的檢查,適用於在網頁檢視畫面外部導入廣告的開發人員。如果您略過這個步驟,且未提供GADApplicationIdentifier
,Google Mobile Ads SDK 就會在應用程式啟動時擲回GADInvalidInitializationException
。<!-- Indicate Google Mobile Ads SDK usage is only for web view APIs for ads --> <key>GADIntegrationManager</key> <string>webview</string>
註冊網頁檢視畫面
在主執行緒上呼叫 register(_:)
,即可與 AdSense 程式碼中的 JavaScript 處理常式或每個 WKWebView
例項中的 Google 發布者代碼建立連線。應盡早完成此操作,例如在 View Controller 的 viewDidLoad
方法中。
Swift
import WebKit
class ViewController: UIViewController {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Initialize a WKWebViewConfiguration object.
let webViewConfiguration = WKWebViewConfiguration()
// Let HTML videos with a "playsinline" attribute play inline.
webViewConfiguration.allowsInlineMediaPlayback = true
// Let HTML videos with an "autoplay" attribute play automatically.
webViewConfiguration.mediaTypesRequiringUserActionForPlayback = []
// Initialize the WKWebView with your WKWebViewConfiguration object.
webView = WKWebView(frame: view.frame, configuration: webViewConfiguration)
view.addSubview(webView)
// Register the web view.
GADMobileAds.sharedInstance().register(webView)
}
}
Objective-C
@import WebKit;
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic, strong) WKWebView *webView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Initialize a WKWebViewConfiguration object.
WKWebViewConfiguration *webViewConfiguration = [[WKWebViewConfiguration alloc] init];
// Let HTML videos with a "playsinline" attribute play inline.
webViewConfiguration.allowsInlineMediaPlayback = YES;
// Let HTML videos with an "autoplay" attribute play automatically.
webViewConfiguration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
// Initialize the WKWebView with your WKWebViewConfiguration object.
self.webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:webViewConfiguration];
[self.view addSubview:self.webView];
// Register the web view.
[GADMobileAds.sharedInstance registerWebView:self.webView];
}
測試整合功能
在使用自訂網址之前,建議您先載入下列網址來測試整合:
https://webview-api-for-ads-test.glitch.me#api-for-ads-tests
在下列情況下,測試網址會顯示成功整合的綠色狀態列:
WKWebView
已連結至 Google Mobile Ads SDK
後續步驟
- 在
WKWebView
中收集同意聲明。廣告的 Web 檢視畫面 API 不會將使用 IAB 資訊公開和同意聲明架構第 2.0 版或 IAB 消費者隱私保護法案 法規遵循架構,在行動應用程式情境中收集到的同意聲明,傳播至網頁檢視畫面中的代碼。如果您想在WKWebView
和對應的營利網站內容中,以單一同意聲明流程收集同意聲明,請透過同意聲明管理平台,在WKWebView
情境中收集同意聲明。