廣告專用的網頁檢視 API 可讓應用程式信號提供給標記,有助於改善提供內容的發布商營利,並保護廣告主免受垃圾內容干擾。
WKWebView
運作方式
只有在下列任一情況觸發廣告事件時,才會與 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(_:)
,與每個 WKWebView
執行個體中的 AdSense 程式碼或 Google 發布商代碼建立 JavaScript 處理常式連線。請盡早完成這項操作,例如在檢視畫面控制器的 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.
MobileAds.shared.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://google.github.io/webview-ads/test/#api-for-ads-tests
如果符合下列條件,測試網址會顯示綠色狀態列,表示整合成功:
WKWebView
已連結至 Google Mobile Ads SDK
後續步驟
- 在
WKWebView
中收集同意聲明。廣告適用的 WebView API 不會使用 IAB 資訊公開和同意聲明架構第 2.0 版或 IAB CCPA 法規遵循架構,將在行動應用程式環境中收集到的同意聲明傳播至 WebView 中的代碼。如果您是WKWebView
和相應營利網站內容的擁有者,並有意導入單一同意聲明流程,請與同意聲明管理平台合作,在WKWebView
環境中收集同意聲明。