หากแอปของคุณใช้
WKWebView
เพื่อแสดงเนื้อหาเว็บ คุณอาจต้องพิจารณาเพิ่มประสิทธิภาพลักษณะการคลิกด้วยเหตุผลต่อไปนี้
-
WKWebView
ไม่รองรับการท่องเว็บแบบแท็บ การคลิกโฆษณาที่พยายามเปิดแท็บใหม่จะไม่ทําอะไรเลยโดยค่าเริ่มต้น
การคลิกโฆษณาที่เปิดในแท็บเดียวกันจะโหลดหน้าเว็บซ้ำ คุณอาจต้องบังคับให้คลิกโฆษณาเปิดนอก
WKWebView
เช่น หากคุณโฮสต์เกม H5 และต้องการรักษาสถานะของเกมแต่ละเกมฟีเจอร์ป้อนข้อความอัตโนมัติไม่รองรับข้อมูลบัตรเครดิตใน
WKWebView
ซึ่งอาจทําให้ Conversion อีคอมเมิร์ซของผู้ลงโฆษณาลดลง ซึ่งส่งผลเสียต่อการสร้างรายได้จากเนื้อหาเว็บ
- Google Sign-In ไม่รองรับใน
WKWebView
คู่มือนี้จะแสดงขั้นตอนที่แนะนําในการเพิ่มประสิทธิภาพลักษณะการคลิกใน WebView บนอุปกรณ์เคลื่อนที่ ขณะเดียวกันก็รักษาเนื้อหา WebView ไว้
ข้อกำหนดเบื้องต้น
- ทำตามคู่มือตั้งค่าเว็บวิวให้เสร็จสมบูรณ์
การใช้งาน
ลิงก์โฆษณาสามารถตั้งค่าแอตทริบิวต์เป้าหมาย 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
อินสแตนซ์
ตั้งค่า
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? {
// 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 ที่ต้องการ
- เมื่อกลับไปที่แอป ตัวนับของหน้าทดสอบจะไม่รีเซ็ตเป็น 0 เพื่อตรวจสอบว่าสถานะของหน้าเว็บได้รับการเก็บรักษาไว้