앱에서 WKWebView를 사용하여 웹 콘텐츠를 표시하는 경우 다음과 같은 이유로 클릭 동작을 최적화하는 것이 좋습니다.
- WKWebView는 탭 브라우징을 지원하지 않습니다. 새 탭을 열려고 시도하는 광고 클릭은 기본적으로 아무것도 하지 않습니다.
동일한 탭에서 열리는 광고 클릭은 페이지를 새로고침합니다. 광고 클릭이
WKWebView
외부에서 열리도록 강제할 수 있습니다. 예를 들어 H5 게임을 호스팅하고 각 게임의 상태를 유지하려는 경우를 들 수 있습니다.자동 완성은
WKWebView
의 신용카드 정보를 지원하지 않습니다. 이로 인해 광고주의 전자상거래 전환수가 줄어 웹 콘텐츠의 수익 창출에 부정적인 영향을 미칠 수 있습니다.
WKWebView
에서는 Google 로그인이 지원되지 않습니다.
이 가이드에서는 웹 뷰 콘텐츠를 유지하면서 모바일 웹 뷰의 클릭 동작을 최적화하는 데 권장되는 단계를 제공합니다.
기본 요건
- WebView 설정 가이드를 완료합니다.
구현
광고 링크의 href
타겟 속성을 _blank
, _top
, _self
또는 _parent
로 설정할 수 있습니다.
Ad Manager를 사용하면 광고가 새 탭 또는 창에서 열리도록 설정하여 타겟 속성을 _blank
또는 _top
로 제어할 수 있습니다.
광고 링크에는 window.open(url, "_blank")
와 같은 JavaScript 함수도 포함될 수 있습니다.
다음 표에서는 이러한 각 링크가 WebView에서 어떻게 동작하는지 설명합니다.
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
는 이러한 클릭을 처리할 수 없으므로 이러한 클릭은 수동으로 처리해야 합니다.
외부 브라우저인
SFSafariViewController
에서 URL을 열지 또는 기존 WebView에서 열지 결정합니다. 이 코드 스니펫은SFSafariViewController
를 표시하여 사이트에서 나가도록 URL을 여는 방법을 보여줍니다.
코드 예
다음 코드 스니펫은 WebView 클릭 동작을 최적화하는 방법을 보여줍니다. 예를 들어 현재 도메인이 대상 도메인과 다른지 확인합니다. 사용하는 기준은 다를 수 있으므로 이는 한 가지 접근 방식일 뿐입니다.
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으로 재설정되지 않아 페이지 상태가 보존되었는지 확인할 수 없습니다.