تحسين سلوك النقر في WKWebView

إذا كان تطبيقك يستخدم WKWebView لعرض محتوى الويب، ننصحك بتحسين سلوك النقرات للأسباب التالية:

  • لا يتيح WKWebView التصفّح باستخدام علامات تبويب. لا تؤدي النقرات على الإعلانات التي تحاول فتح علامة تبويب جديدة إلى أيّ إجراء بشكلٍ تلقائي.

  • تؤدي النقرات على الإعلانات التي يتم فتحها في علامة التبويب نفسها إلى إعادة تحميل الصفحة. قد تحتاج إلى فرض فتح النقرات على الإعلانات خارج WKWebView، على سبيل المثال إذا كنت تستضيف ألعابًا بلغة HTML5 وتريد الحفاظ على حالة كل لعبة.

  • لا تتيح ميزة "الملء التلقائي" استخدام معلومات بطاقة الائتمان في WKWebView. وقد يؤدي ذلك بدوره إلى خفض عدد الإحالات الناجحة الناتجة عن التجارة الإلكترونية للمعلِنين، ما يؤثّر سلبًا في إمكانية تحقيق الربح من محتوى الويب.

يقدّم هذا الدليل الخطوات المقترَحة لتحسين سلوك النقر في مشاهدات الويب على الأجهزة الجوّالة مع الحفاظ على محتوى عرض الويب.

المتطلبات الأساسية

التنفيذ

يمكن ضبط سمة الوجهة href في روابط الإعلانات على _blank أو _top أو _self أو _parent. يمكن أن تحتوي روابط الإعلانات أيضًا على وظائف JavaScript مثل window.open(url, "_blank").

يوضّح الجدول التالي سلوك كلّ رابط من هذه الروابط في عرض الويب.

سمة الاستهداف href السلوك التلقائي للنقر على WKWebView
target="_blank" لم تتم معالجة الرابط من خلال WebView.
target="_top" أعِد تحميل الرابط في عرض الويب الحالي.
target="_self" أعِد تحميل الرابط في طريقة عرض الويب الحالية.
target="_parent" أعِد تحميل الرابط في عرض الويب الحالي.
دالة JavaScript السلوك التلقائي للنقر على WKWebView
window.open(url, "_blank") لم تتم معالجة الرابط من خلال WebView.

اتّبِع الخطوات التالية لتحسين سلوك النقرات في WKWebView مثيلك:

  1. اضبط WKUIDelegate على مثيل WKWebView.

  2. اضبط WKNavigationDelegate على مثيل WKWebView.

  3. حدِّد ما إذا كنت تريد تحسين سلوك عنوان URL للنقر.

    • تحقّق مما إذا كان موقع navigationType في كائن WKNavigationAction هو نوع نقرة تريد تحسينه. يتحقّق مثال الرمز من .linkActivated الذي لا ينطبق إلا على النقرات على رابط يحتوي على سمة href.

    • تحقّق من سمة targetFrame في عنصر WKNavigationAction. إذا تم عرض nil، يعني ذلك أنّه هدف التنقّل هو نافذة جديدة. بما أنّ WKWebView لا يمكنه التعامل مع هذه النقرة، يجب معالجة هذه النقرات يدويًا.

  4. حدِّد ما إذا كنت تريد فتح عنوان 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 المقصود.
  • عند العودة إلى التطبيق، لا تتم إعادة ضبط عداد الصفحة التجريبية على القيمة صفر للتأكّد من الحفاظ على حالة الصفحة.