אם האפליקציה שלכם משתמשת ב-WKWebView כדי להציג תוכן מהאינטרנט, כדאי לשקול לבצע אופטימיזציה של מה שקורה כשהמשתמש לוחץ על מודעה, מהסיבות הבאות:
-
WKWebViewלא תומך בגלישה עם כרטיסיות. קליקים על מודעות שמנסים לפתוח כרטיסייה חדשה לא עושים כלום כברירת מחדל.
קליקים על מודעות שנפתחות באותה כרטיסייה גורמים לטעינה מחדש של הדף. לפעמים כדאי להגדיר שלחיצה על מודעות תפתח אותן מחוץ ל-
WKWebView, למשל אם אתם מארחים משחקי H5 ורוצים לשמור על המצב של כל משחק.המילוי האוטומטי לא תומך בפרטי כרטיס אשראי ב-
WKWebView. זה יכול להוביל לירידה במספר ההמרות של מסחר אלקטרוני למפרסמים, ולהשפיע לרעה על המונטיזציה של התוכן האינטרנטי.
- אי אפשר להשתמש בהתחברות באמצעות חשבון Google ב-
WKWebView.
במדריך הזה מפורטים שלבים מומלצים לאופטימיזציה של מה שקורה כשהמשתמש לוחץ על מודעה בתצוגות של אינטרנט לנייד, תוך שמירה על התוכן של תצוגת האינטרנט.
דרישות מוקדמות
- בצעו את ההוראות שבמדריך להגדרת תצוגת אינטרנט.
הטמעה
ערך המאפיין href של קישורי מודעות יכול להיות _blank, _top, _self או _parent.
קישורים למודעות יכולים להכיל גם פונקציות 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:).
- מטמיעים את
מחליטים אם לבצע אופטימיזציה של הפעולה שגורמת כתובת האתר לקליקים.
בודקים אם המאפיין
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? {
// 3. Determine whether to optimize the behavior of the click URL.
if didHandleClickBehavior(
currentURL: webView.url,
navigationAction: navigationAction) {
print("URL opened in SFSafariViewController.")
}
return nil
}
// Implement the WKNavigationDelegate method.
func webView(
_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
{
// 3. Determine whether to optimize the behavior of the click URL.
if didHandleClickBehavior(
currentURL: webView.url,
navigationAction: navigationAction) {
return decisionHandler(.cancel)
}
decisionHandler(.allow)
}
// Implement a helper method to handle click behavior.
func didHandleClickBehavior(
currentURL: URL,
navigationAction: WKNavigationAction) -> Bool {
guard let targetURL = navigationAction.request.url else {
return false
}
// Handle custom URL schemes such as itms-apps:// by attempting to
// launch the corresponding application.
if navigationAction.navigationType == .linkActivated {
if let scheme = targetURL.scheme, !["http", "https"].contains(scheme) {
UIApplication.shared.open(targetURL, options: [:], completionHandler: nil)
return true
}
}
guard let currentDomain = currentURL.host,
let targetDomain = targetURL.host else {
return false
}
// 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 == .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 {
// 4. Open the URL in a SFSafariViewController.
let safariViewController = SFSafariViewController(url: targetURL)
present(safariViewController, animated: true)
return true
}
return false
}
}
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 {
// 3. Determine whether to optimize the behavior of the click URL.
if ([self didHandleClickBehaviorForCurrentURL: webView.URL
navigationAction: navigationAction]) {
NSLog(@"URL opened in SFSafariViewController.");
}
return nil;
}
// Implement the WKNavigationDelegate method.
- (void)webView:(WKWebView *)webView
decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
decisionHandler:
(void (^)(WKNavigationActionPolicy))decisionHandler {
// 3. Determine whether to optimize the behavior of the click URL.
if ([self didHandleClickBehaviorForCurrentURL: webView.URL
navigationAction: navigationAction]) {
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
// Implement a helper method to handle click behavior.
- (BOOL)didHandleClickBehaviorForCurrentURL:(NSURL *)currentURL
navigationAction:(WKNavigationAction *)navigationAction {
NSURL *targetURL = navigationAction.request.URL;
// Handle custom URL schemes such as itms-apps:// by attempting to
// launch the corresponding application.
if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
NSString *scheme = targetURL.scheme;
if (![scheme isEqualToString:@"http"] && ![scheme isEqualToString:@"https"]) {
[UIApplication.sharedApplication openURL:targetURL options:@{} completionHandler:nil];
return YES;
}
}
NSString *currentDomain = currentURL.host;
NSString *targetDomain = targetURL.host;
if (!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:targetURL];
[self presentViewController:safariViewController animated:YES
completion:nil];
return YES;
}
return NO;
}
בדיקת הניווט בדפים
כדי לבדוק את השינויים בניווט בדפים, טוענים
https://google.github.io/webview-ads/test/#click-behavior-tests
לתצוגת האינטרנט. כדי לראות מה קורה באפליקציה כשלוחצים על כל סוג של קישור, לחצו על הסוג הרלוונטי.
הנה כמה דברים שכדאי לבדוק:
- כל קישור פותח את כתובת ה-URL הרצויה.
- כשחוזרים לאפליקציה, המונה בדף הבדיקה לא מתאפס כדי לאמת שהמצב של הדף נשמר.