Jeśli iOS aplikacja wykorzystujeWKWebView
do wyświetlania treści internetowych, warto rozważyć optymalizację zachowania po kliknięciu z tych powodów:
WKWebView
nie obsługuje Przeglądanie na kartach. Kliknięcia reklam, które próbują otworzyć nową kartę, domyślnie nie prowadzą do żadnych działań.
Kliknięcia reklam, które otwierają się na tej samej karcie, ponownie załadują stronę. Możesz wymusić otwieranie reklam poza obszarem
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 prowadzić do zmniejszenia liczby konwersji e-commerce u reklamodawców, co negatywnie wpłynie na przychody z treści internetowych.
- Logowanie przez Google
obsługiwane w
WKWebView
.
W tym przewodniku znajdziesz zalecane czynności, które pozwolą Ci zoptymalizować zachowanie kliknięć w widokach stron mobilnych przy zachowaniu zawartości widoku witryny.
Wymagania wstępne
- Wykonaj instrukcje konfigurowania widoku witryny.
Implementacja
W przypadku linków reklam atrybut docelowy href
może mieć wartość _blank
, _top
, _self
lub _parent
.
W usłudze Ad Manager możesz ustawić atrybut docelowy _blank
lub _top
, ustawiając otwieranie reklam w nowej karcie lub nowym oknie.
Linki w reklamach mogą też zawierać funkcje JavaScript, np. window.open(url, "_blank")
.
W tabeli poniżej opisano, jak każdy z tych linków zachowuje się w widoku witryny.
href atrybut docelowy |
Domyślne zachowanie związane z kliknięciami: WKWebView |
---|---|
target="_blank" |
Link nieobsługiwany przez widok witryny. |
target="_top" |
Ponownie załaduj link w istniejącym widoku witryny. |
target="_self" |
Ponownie załaduj link w istniejącym widoku witryny. |
target="_parent" |
Ponownie załaduj link w istniejącym widoku witryny. |
Funkcja JavaScript | Domyślne zachowanie związane z kliknięciami: WKWebView |
window.open(url, "_blank") |
Link nieobsługiwany przez widok witryny. |
Aby zoptymalizować zachowanie po kliknięciu w wystąpieniuWKWebView
:
Ustaw
WKUIDelegate
w instancjiWKWebView
.Ustaw
WKNavigationDelegate
w instancjiWKWebView
.Określ, czy chcesz optymalizować działanie klikanego adresu URL.
Sprawdź, czy właściwość
navigationType
w obiekcieWKNavigationAction
jest typem kliknięcia, który chcesz optymalizować. Fragment kodu sprawdza element.linkActivated
, który dotyczy tylko kliknięć linku z atrybutemhref
.Sprawdź właściwość
targetFrame
w obiekcieWKNavigationAction
. Jeśli zwraca wartośćnil
, oznacza to, że miejscem docelowym nawigacji jest nowe okno.WKWebView
nie obsługuje tego kliknięcia, więc kliknięcia te trzeba obsługiwać ręcznie.
Zdecyduj, czy chcesz otworzyć adres URL w zewnętrznej przeglądarce,
SFSafariViewController
czy w istniejącym widoku witryny. Fragment kodu pokazuje, jak otwierać adresy URL, które opuszczają witrynę, prezentując elementSFSafariViewController
.
Przykładowy kod
Poniższy fragment kodu pokazuje, jak zoptymalizować zachowanie po kliknięciu widoku witryny. Sprawdza na przykład, czy bieżąca domena różni się od domeny docelowej. To tylko jedna z metod, ponieważ kryteria mogą być różne.
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 po stronie
Aby przetestować zmiany w nawigacji po stronie, wczytaj
https://webview-api-for-ads-test.glitch.me#click-behavior-tests
w widoku witryny. Klikaj poszczególne typy linków, aby sprawdzić, jak działają w Twojej aplikacji.
Oto kilka rzeczy, które możesz sprawdzić:
- Każdy link otwiera zamierzony adres URL.
- Po powrocie do aplikacji licznik strony testowej nie resetuje się do zera, co pozwala sprawdzić, czy stan strony został zachowany.