หากแอปของคุณใช้
WKWebView
เพื่อแสดงเนื้อหาเว็บ คุณอาจต้องพิจารณาเพิ่มประสิทธิภาพลักษณะการคลิกด้วยเหตุผลต่อไปนี้
-
WKWebView
ไม่รองรับการท่องเว็บแบบแท็บ การคลิกโฆษณาที่พยายามเปิดแท็บใหม่จะไม่ทําอะไรเลยโดยค่าเริ่มต้น
การคลิกโฆษณาที่เปิดในแท็บเดียวกันจะโหลดหน้าเว็บซ้ำ คุณอาจต้องบังคับให้คลิกโฆษณาเปิดนอก
WKWebView
เช่น หากคุณโฮสต์เกม H5 และต้องการรักษาสถานะของเกมแต่ละเกมฟีเจอร์ป้อนข้อความอัตโนมัติไม่รองรับข้อมูลบัตรเครดิตใน
WKWebView
ซึ่งอาจทําให้ Conversion อีคอมเมิร์ซของผู้ลงโฆษณาลดลง ซึ่งส่งผลเสียต่อการสร้างรายได้จากเนื้อหาเว็บ
- Google Sign-In ไม่รองรับใน
WKWebView
คู่มือนี้จะแสดงขั้นตอนที่แนะนําในการเพิ่มประสิทธิภาพลักษณะการคลิกใน WebView บนอุปกรณ์เคลื่อนที่ ขณะเดียวกันก็รักษาเนื้อหา WebView ไว้
ข้อกำหนดเบื้องต้น
- ทำตามคู่มือตั้งค่าเว็บวิวให้เสร็จสมบูรณ์
การใช้งาน
ลิงก์โฆษณาสามารถตั้งค่าแอตทริบิวต์เป้าหมาย href
เป็น _blank
, _top
, _self
หรือ _parent
เมื่อใช้ Ad Manager คุณสามารถควบคุมแอตทริบิวต์เป้าหมายเป็น _blank
หรือ _top
ได้โดยการตั้งค่าให้โฆษณาเปิดในแท็บหรือหน้าต่างใหม่
ลิงก์โฆษณาอาจมีฟังก์ชัน JavaScript ด้วย เช่น window.open(url, "_blank")
ตารางต่อไปนี้อธิบายลักษณะการทำงานของลิงก์แต่ละรายการในมุมมองเว็บ
แอตทริบิวต์เป้าหมาย href |
ลักษณะการคลิก WKWebView เริ่มต้น |
---|---|
target="_blank" |
ลิงก์ที่ WebView จัดการไม่ได้ |
target="_top" |
โหลดลิงก์ซ้ำในมุมมองเว็บที่มีอยู่ |
target="_self" |
โหลดลิงก์ซ้ำในมุมมองเว็บที่มีอยู่ |
target="_parent" |
โหลดลิงก์ซ้ำในมุมมองเว็บที่มีอยู่ |
ฟังก์ชัน JavaScript | ลักษณะการคลิก WKWebView เริ่มต้น |
window.open(url, "_blank") |
ลิงก์ที่ WebView จัดการไม่ได้ |
ทําตามขั้นตอนต่อไปนี้เพื่อเพิ่มประสิทธิภาพลักษณะการคลิกใน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 ที่ต้องการ
- เมื่อกลับไปที่แอป ตัวนับของหน้าทดสอบจะไม่รีเซ็ตเป็น 0 เพื่อตรวจสอบว่าสถานะของหน้าเว็บได้รับการเก็บรักษาไว้