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

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

  • WKWebView لا يتوافق مع باستخدام علامات التبويب. النقرات على الإعلان التي تحاول فتح علامة تبويب جديدة لا تتّخذ أي إجراء تلقائيًا.

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

  • لا يتوافق الملء التلقائي مع بيانات بطاقة الائتمان في 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 المثيل:

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