אם האפליקציה שלכם משתמשת ב-WKWebView
כדי להציג תוכן אינטרנט, כדאי לשקול לבצע אופטימיזציה של התנהגות הקליקים מהסיבות הבאות:
-
בדפדפן
WKWebView
אין תמיכה בדפדוף בכרטיסיות. לחיצות על מודעות שמנסות לפתוח כרטיסייה חדשה לא מבצעות שום פעולה כברירת מחדל.
קליקים על מודעות שנפתחות באותה כרטיסייה יגרמו לטעינה מחדש של הדף. יכול להיות שתרצו לאלץ את הקליקים על המודעות להיפתח מחוץ ל-
WKWebView
, למשל אם אתם מארחים משחקי H5 ואתם רוצים לשמור על המצב של כל משחק.המילוי האוטומטי לא תומך בפרטי כרטיס אשראי ב-
WKWebView
. כתוצאה מכך, המפרסמים עשויים לקבל פחות המרות מסחר אלקטרוני, דבר שמשפיע לרעה על המונטיזציה של תוכן האינטרנט.
- אי אפשר להשתמש בכניסה באמצעות חשבון Google ב-
WKWebView
.
במדריך הזה מפורטות שלבים מומלצים לביצוע אופטימיזציה של התנהגות הקליקים בתצוגות אינטרנט בנייד, תוך שמירה על התוכן בתצוגת האינטרנט.
דרישות מוקדמות
- פועלים לפי ההוראות במדריך הגדרת תצוגת האינטרנט.
הטמעה
אפשר להגדיר את מאפיין היעד href
של קישורי מודעות לערך _blank
, _top
, _self
או _parent
.
ב-Ad Manager אפשר לשלוט במאפיין היעד כך שיהיה _blank
או _top
על ידי הגדרת המודעות כך שייפתחו בכרטיסייה או בחלון חדשים.
קישורי מודעות יכולים לכלול גם פונקציות JavaScript, כמו window.open(url, "_blank")
.
בטבלה הבאה מתואר האופן שבו כל אחד מהקישורים האלה פועל בתצוגת אינטרנט.
מאפיין היעד href |
התנהגות הקליקים שמוגדרת כברירת מחדל ב-WKWebView |
---|---|
target="_blank" |
הקישור לא מטופל על ידי תצוגת האינטרנט. |
target="_top" |
טוענים מחדש את הקישור בתצוגת האינטרנט הקיימת. |
target="_self" |
טוענים מחדש את הקישור בתצוגת האינטרנט הקיימת. |
target="_parent" |
טוענים מחדש את הקישור בתצוגת האינטרנט הקיימת. |
פונקציית JavaScript | התנהגות הקליקים שמוגדרת כברירת מחדל ב-WKWebView |
window.open(url, "_blank") |
הקישור לא מטופל על ידי תצוגת האינטרנט. |
כדי לבצע אופטימיזציה של התנהגות הקליקים במכונה של WKWebView
:
מגדירים את
WKUIDelegate
במכונה שלWKWebView
.- מטמיעים את
webView(_:createWebViewWith:for:windowFeatures:)
.
- מטמיעים את
מגדירים את
WKNavigationDelegate
במכונה שלWKWebView
.- מטמיעים את
webView(_:decidePolicyFor:decisionHandler:)
.
- מטמיעים את
קובעים אם לבצע אופטימיזציה של התנהגות כתובת ה-URL של הקליק.
בודקים אם הנכס
navigationType
באובייקטWKNavigationAction
הוא סוג של קליק שרוצים לבצע אופטימיזציה עבורו. דוגמת הקוד בודקת את הערך.linkActivated
, שרלוונטי רק לקליקים על קישור עם מאפייןhref
.בודקים את המאפיין
targetFrame
באובייקטWKNavigationAction
. אם הפונקציה מחזירה את הערךnil
, המשמעות היא שהיעד של הניווט הוא חלון חדש. מכיוון ש-WKWebView
לא יכול לטפל בקליק הזה, צריך לטפל בקליקים האלה באופן ידני.
מחליטים אם לפתוח את כתובת ה-URL בדפדפן חיצוני,
SFSafariViewController
או בתצוגת האינטרנט הקיימת. קטע הקוד הזה מראה איך לפתוח כתובות URL שמובילות מחוץ לאתר באמצעות הצגתSFSafariViewController
.
קוד לדוגמה
קטע הקוד הבא מראה איך לבצע אופטימיזציה של התנהגות הקליקים בתצוגת האינטרנט. לדוגמה, המערכת בודקת אם הדומיין הנוכחי שונה מהדומיין היעד. זו רק גישה אחת, כי הקריטריונים שבהם אתם משתמשים עשויים להשתנות.
Swift
import GoogleMobileAds
import SafariServices
import WebKit
class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// ... Register the WKWebView.
// 1. Set the WKUIDelegate on your WKWebView instance.
webView.uiDelegate = self;
// 2. Set the WKNavigationDelegate on your WKWebView instance.
webView.navigationDelegate = self
}
// Implement the WKUIDelegate method.
func webView(
_ webView: WKWebView,
createWebViewWith configuration: WKWebViewConfiguration,
for navigationAction: WKNavigationAction,
windowFeatures: WKWindowFeatures) -> WKWebView? {
guard let url = navigationAction.request.url,
let currentDomain = webView.url?.host,
let targetDomain = url.host else { return nil }
// 3. Determine whether to optimize the behavior of the click URL.
if didHandleClickBehavior(
url: url,
currentDomain: currentDomain,
targetDomain: targetDomain,
navigationAction: navigationAction) {
print("URL opened in SFSafariViewController.")
}
return nil
}
// Implement the WKNavigationDelegate method.
func webView(
_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
{
guard let url = navigationAction.request.url,
let currentDomain = webView.url?.host,
let targetDomain = url.host else { return decisionHandler(.cancel) }
// 3. Determine whether to optimize the behavior of the click URL.
if didHandleClickBehavior(
url: url,
currentDomain: currentDomain,
targetDomain: targetDomain,
navigationAction: navigationAction) {
return decisionHandler(.cancel)
}
decisionHandler(.allow)
}
// Implement a helper method to handle click behavior.
func didHandleClickBehavior(
url: URL,
currentDomain: String,
targetDomain: String,
navigationAction: WKNavigationAction) -> Bool {
// Check if the navigationType is a link with an href attribute or
// if the target of the navigation is a new window.
guard navigationAction.navigationType == .linkActivated ||
navigationAction.targetFrame == nil,
// If the current domain does not equal the target domain,
// the assumption is the user is navigating away from the site.
currentDomain != targetDomain else { return false }
// 4. Open the URL in a SFSafariViewController.
let safariViewController = SFSafariViewController(url: url)
present(safariViewController, animated: true)
return true
}
}
Objective-C
@import GoogleMobileAds;
@import SafariServices;
@import WebKit;
@interface ViewController () <WKNavigationDelegate, WKUIDelegate>
@property(nonatomic, strong) WKWebView *webView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// ... Register the WKWebView.
// 1. Set the WKUIDelegate on your WKWebView instance.
self.webView.uiDelegate = self;
// 2. Set the WKNavigationDelegate on your WKWebView instance.
self.webView.navigationDelegate = self;
}
// Implement the WKUIDelegate method.
- (WKWebView *)webView:(WKWebView *)webView
createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration
forNavigationAction:(WKNavigationAction *)navigationAction
windowFeatures:(WKWindowFeatures *)windowFeatures {
NSURL *url = navigationAction.request.URL;
NSString *currentDomain = webView.URL.host;
NSString *targetDomain = navigationAction.request.URL.host;
// 3. Determine whether to optimize the behavior of the click URL.
if ([self didHandleClickBehaviorForURL: url
currentDomain: currentDomain
targetDomain: targetDomain
navigationAction: navigationAction]) {
NSLog(@"URL opened in SFSafariViewController.");
}
return nil;
}
// Implement the WKNavigationDelegate method.
- (void)webView:(WKWebView *)webView
decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
decisionHandler:
(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURL *url = navigationAction.request.URL;
NSString *currentDomain = webView.URL.host;
NSString *targetDomain = navigationAction.request.URL.host;
// 3. Determine whether to optimize the behavior of the click URL.
if ([self didHandleClickBehaviorForURL: url
currentDomain: currentDomain
targetDomain: targetDomain
navigationAction: navigationAction]) {
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
// Implement a helper method to handle click behavior.
- (BOOL)didHandleClickBehaviorForURL:(NSURL *)url
currentDomain:(NSString *)currentDomain
targetDomain:(NSString *)targetDomain
navigationAction:(WKNavigationAction *)navigationAction {
if (!url || !currentDomain || !targetDomain) {
return NO;
}
// Check if the navigationType is a link with an href attribute or
// if the target of the navigation is a new window.
if ((navigationAction.navigationType == WKNavigationTypeLinkActivated
|| !navigationAction.targetFrame)
// If the current domain does not equal the target domain,
// the assumption is the user is navigating away from the site.
&& ![currentDomain isEqualToString: targetDomain]) {
// 4. Open the URL in a SFSafariViewController.
SFSafariViewController *safariViewController =
[[SFSafariViewController alloc] initWithURL:url];
[self presentViewController:safariViewController animated:YES
completion:nil];
return YES;
}
return NO;
}
בדיקת הניווט בדפים
כדי לבדוק את השינויים בניווט בדף, צריך לטעון
https://webview-api-for-ads-test.glitch.me#click-behavior-tests
בתצוגת האינטרנט. אפשר ללחוץ על כל אחד מסוגי הקישורים השונים כדי לראות איך הם מתנהגים באפליקציה.
הנה כמה דברים שכדאי לבדוק:
- כל קישור פותח את כתובת ה-URL הרצויה.
- כשחוזרים לאפליקציה, המונה של דף הבדיקה לא מתאפס כדי לאמת שסטטוס הדף נשמר.