アプリで WKWebView を使用してウェブ コンテンツを表示している場合は、次の理由からクリック動作の最適化を検討することをおすすめします。
- WKWebView はタブ ブラウジングをサポートしていません。新しいタブを開こうとする広告クリックは、デフォルトでは何もしません。
同じタブで開いた広告をクリックすると、ページが再読み込みされます。H5 ゲームをホストしていて、各ゲームの状態を維持したい場合など、広告クリックを
WKWebView
の外部で強制的に開くようにしたい場合があります。自動入力では、
WKWebView
のクレジット カード情報はサポートされていません。これにより、広告主様の e コマース コンバージョンが減少し、ウェブ コンテンツの収益化に悪影響を及ぼす可能性があります。
WKWebView
では Google ログインはサポートされていません。
このガイドでは、ウェブビューのコンテンツを維持しながら、モバイル ウェブビューのクリック動作を最適化するための推奨手順について説明します。
前提条件
- ウェブビューを設定するガイドの手順を完了します。
実装
広告リンクの href
ターゲット属性は、_blank
、_top
、_self
、_parent
のいずれかに設定できます。アド マネージャーでは、広告を新しいタブまたはウィンドウで開くように設定することで、ターゲット属性を _blank
または _top
に設定できます。広告リンクには、window.open(url, "_blank")
などの JavaScript 関数を含めることもできます。
次の表に、これらのリンクがウェブビューでどのように動作するかを示します。
href ターゲット属性 |
デフォルトの WKWebView クリック動作 |
---|---|
target="_blank" |
リンクがウェブビューで処理されない。 |
target="_top" |
既存のウェブビューでリンクを再読み込みします。 |
target="_self" |
既存のウェブビューでリンクを再読み込みします。 |
target="_parent" |
既存のウェブビューでリンクを再読み込みします。 |
JavaScript 関数 | デフォルトの WKWebView クリック動作 |
window.open(url, "_blank") |
リンクがウェブビューで処理されない。 |
WKWebView
インスタンスのクリック動作を最適化する手順は次のとおりです。
WKWebView
インスタンスにWKUIDelegate
を設定します。WKWebView
インスタンスにWKNavigationDelegate
を設定します。クリック URL の動作を最適化するかどうかを決定します。
WKNavigationAction
オブジェクトのnavigationType
プロパティが、最適化したいクリックタイプかどうかを確認します。コード例では、href
属性を持つリンクのクリックにのみ適用される.linkActivated
を確認します。WKNavigationAction
オブジェクトのtargetFrame
プロパティを確認します。nil
が返された場合は、ナビゲーションのターゲットが新しいウィンドウであることを意味します。WKWebView
はそのようなクリックを処理できないため、これらのクリックは手動で処理する必要があります。
URL を外部ブラウザ(
SFSafariViewController
)で開くか、既存のウェブビューで開くかを決定します。次のコード スニペットは、SFSafariViewController
を表示してサイトから離れる URL を開く方法を示しています。
サンプルコード
次のコード スニペットは、ウェブビューのクリック動作を最適化する方法を示しています。たとえば、現在のドメインが移行先ドメインと異なるかどうかを確認します。使用する基準は異なる可能性があるため、これはあくまで 1 つのアプローチにすぎません。
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 を開きます。
- アプリに戻ると、テストページのカウンタがゼロにリセットされず、ページの状態が保持されていることを検証します。