ממשקי ה-API של WebView למודעות מאפשרים לאותות מהאפליקציה להיות זמינים לתגים ב-WKWebView, וכך בעלי תוכן דיגיטלי יכולים לשפר את המונטיזציה ולהגן על מפרסמים מפני ספאם. האותות האלה מהאפליקציה, כמו מזהה האפליקציה וגרסת האפליקציה, עוזרים להפעיל תרחישים לדוגמה של דיווח וטירגוט מלאי שטחי פרסום בדפדפן באפליקציה, שבדרך כלל זמינים רק בתנועה באפליקציה.
איך זה עובד
התקשורת עם Google Mobile Ads SDK מתרחשת רק בתגובה לאירועי מודעות שמופעלים על ידי אחד מהגורמים הבאים:
ה-SDK מוסיף פונקציות לטיפול בהודעות ל-WKWebView הרשום כדי להאזין לאירועי המודעות האלה. כדי להבין טוב יותר איך זה עובד, אפשר לעיין בקוד המקור של דף הבדיקה.
דרישות מוקדמות
- Google Mobile Ads SDK בגרסה 9.6.0 ואילך.
מעדכנים את הקובץ
Info.plistעם המפתח וערך המחרוזת הבאים. כך אפשר לעקוף בדיקה של Google Mobile Ads SDK לגבי ערך שלGADApplicationIdentifierשרלוונטי למפתחים שמטמיעים מודעות מחוץ ל-WebView. אם לא תבצעו את השלב הזה ולא תספקו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(_:) בשרשור הראשי כדי ליצור חיבור עם ה-handlers של JavaScript בקוד AdSense או ב-Google Publisher Tag בכל מופע של WKWebView. צריך לעשות את זה בשלב מוקדם ככל האפשר, למשל ב-method viewDidLoad של ה-view controller.
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];
}
בדיקת השילוב
לפני שאתם משתמשים בכתובת URL משלכם, מומלץ לטעון את כתובת ה-URL הבאה כדי לבדוק את השילוב:
https://google.github.io/webview-ads/test/#api-for-ads-tests
אם התנאים הבאים מתקיימים, כתובת ה-URL לבדיקה מציגה שורות סטטוס ירוקות לשילוב מוצלח:
-
WKWebViewמחובר אל Google Mobile Ads SDK
השלבים הבאים
- קבלת הסכמה ב-
WKWebView. ממשקי ה-API של WebView למודעות לא מעבירים את ההסכמה שנאספה בהקשר של האפליקציה לנייד באמצעות מסגרות התאימות IAB TCF v2.3 או IAB CCPA לתגים בתצוגות WebView. אם אתם רוצים להטמיע תהליך הסכמה אחד בתור הבעלים שלWKWebViewושל תוכן האינטרנט המתאים שמופעלת בו מונטיזציה, אתם צריכים לעבוד עם הפלטפורמה לניהול הסכמה כדי לאסוף הסכמה בהקשר שלWKWebView.