Jeśli Twoja aplikacja korzysta z WKWebView
do wyświetlania treści internetowych, warto rozważyć optymalizację zachowania po kliknięciu z tych powodów:
-
WKWebView
nie obsługuje przeglądania w kartach. Kliknięcia reklam, które próbują otworzyć nową kartę, domyślnie niczego nie robią.
Kliknięcia reklam, które otwierają się w tej samej karcie, powodują ponowne wczytywanie strony. Możesz zmusić Ad Clicks do otwierania się poza
WKWebView
, na przykład jeśli hostujesz gry H5 i chcesz zachować stan każdej gry.Autouzupełnianie nie obsługuje danych kart kredytowych w
WKWebView
. Może to spowodować spadek liczby konwersji e-commerce u reklamodawców, co negatywnie wpłynie na zarabianie na treściach internetowych.
- Logowanie przez Google
nie jest obsługiwane w
WKWebView
.
W tym przewodniku znajdziesz zalecane czynności, które pozwolą Ci zoptymalizować zachowanie kliknięć w widokach stron internetowych na urządzeniach mobilnych, zachowując jednocześnie zawartość widoku strony internetowej.
Wymagania wstępne
- Zapoznaj się z przewodnikiem konfiguracji widoku internetowego.
Implementacja
Atrybut docelowy href
w linkach reklam może mieć wartość _blank
, _top
, _self
lub _parent
.
Linki reklam mogą też zawierać funkcje JavaScriptu, np. window.open(url, "_blank")
.
Tabela poniżej opisuje zachowanie tych linków w widoku internetowym.
Atrybut celu href |
Domyślne zachowanie po kliknięciu WKWebView |
---|---|
target="_blank" |
Link nie jest obsługiwany przez widok internetowy. |
target="_top" |
Załaduj ponownie link w bieżącym widoku internetowym. |
target="_self" |
Załaduj ponownie link w bieżącym widoku internetowym. |
target="_parent" |
Załaduj ponownie link w bieżącym widoku internetowym. |
Funkcja JavaScript | Domyślne zachowanie po kliknięciu WKWebView |
window.open(url, "_blank") |
Link nie jest obsługiwany przez widok internetowy. |
Aby zoptymalizować zachowanie po kliknięciu w przypadku instancji WKWebView
:
Skonfiguruj
WKUIDelegate
w instancjiWKWebView
.Ustaw parametr
WKNavigationDelegate
w instancjiWKWebView
.Określ, czy chcesz optymalizować działanie docelowego adresu URL.
Sprawdź, czy właściwość
navigationType
obiektuWKNavigationAction
to typ kliknięcia, który chcesz optymalizować. Przykład kodu sprawdza, czy występuje.linkActivated
, która ma zastosowanie tylko do kliknięć linku z atrybutemhref
.Sprawdź właściwość
targetFrame
obiektuWKNavigationAction
. Jeśli zwróci wartośćnil
, oznacza to, że element docelowy nawigacji to nowe okno. PonieważWKWebView
nie może obsługiwać tego kliknięcia, musisz je obsługiwać ręcznie.
Zdecyduj, czy otworzyć adres URL w zewnętrznej przeglądarce
SFSafariViewController
, czy w dotychczasowym widoku internetowym. Fragment kodu pokazuje, jak otworzyć adresy URL, przechodząc poza witrynę, prezentującSFSafariViewController
.
Przykładowy kod
Ten fragment kodu pokazuje, jak zoptymalizować zachowanie klikania w widoku internetowym. Na przykład sprawdza, czy bieżąca domena jest inna niż domena docelowa. To tylko jedno z możliwych podejść, ponieważ kryteria mogą się różnić.
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;
}
Testowanie nawigacji na stronie
Aby przetestować zmiany w nawigacji po stronie, otwórz
https://webview-api-for-ads-test.glitch.me#click-behavior-tests
do widoku internetowego. Kliknij każdy z rodzajów linków, aby zobaczyć, jak działają w Twojej aplikacji.
Oto kilka rzeczy, które możesz sprawdzić:
- Każdy link otwiera odpowiedni adres URL.
- Po powrocie do aplikacji licznik na stronie testowej nie jest resetowany do zera, aby sprawdzić, czy stan strony został zachowany.