إذا كان تطبيقك يستخدم 
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.
- اضبط - WKNavigationDelegateعلى مثيل- WKWebView.
- تحديد ما إذا كنت تريد تحسين سلوك عنوان 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? {
    // 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 المقصود.
- عند الرجوع إلى التطبيق، لا تتم إعادة ضبط عدّاد صفحة الاختبار إلى الصفر للتأكّد من الحفاظ على حالة الصفحة.