अगर आपका ऐप्लिकेशन वेब कॉन्टेंट दिखाने के लिए WKWebView का इस्तेमाल करता है, तो आपको इन वजहों से क्लिक व्यवहार को ऑप्टिमाइज़ करने पर विचार करना चाहिए:
- WKWebView में टैब करके ब्राउज़ करने की सुविधा काम नहीं करती. विज्ञापन पर क्लिक करने से नया टैब खुलने की कोशिश की जाती है. हालांकि, डिफ़ॉल्ट रूप से ऐसा नहीं होता.
विज्ञापन पर क्लिक करने से, उसी टैब में पेज फिर से लोड हो जाता है. हो सकता है कि आप विज्ञापन पर मिले क्लिक को
WKWebView
के बाहर खोलने के लिए मजबूर करना चाहें. उदाहरण के लिए, अगर आपने H5 गेम होस्ट किए हैं और आपको हर गेम की स्थिति बनाए रखनी है.ऑटोमैटिक भरने की सुविधा,
WKWebView
में क्रेडिट कार्ड की जानकारी के साथ काम नहीं करती. इससे, विज्ञापन देने वालों के लिए ई-कॉमर्स कन्वर्ज़न की संख्या कम हो सकती है. इससे वेब कॉन्टेंट से कमाई करने पर भी असर पड़ सकता है.
WKWebView
में Google साइन-इन की सुविधा काम नहीं करती.
इस गाइड में, वेब व्यू के कॉन्टेंट को बनाए रखते हुए, मोबाइल वेब व्यू में क्लिक व्यवहार को ऑप्टिमाइज़ करने के लिए सुझाए गए तरीके बताए गए हैं.
ज़रूरी शर्तें
- वेब व्यू सेट अप करने से जुड़ी गाइड को पूरा करें.
लागू करना
विज्ञापन लिंक के लिए, href
टारगेट एट्रिब्यूट को _blank
, _top
, _self
या _parent
पर सेट किया जा सकता है.
Ad Manager की मदद से, विज्ञापनों को नए टैब या विंडो में खोलने की सेटिंग करके, टारगेट एट्रिब्यूट को _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
सेट करें.webView(_:decidePolicyFor:decisionHandler:)
लागू करें.
तय करें कि क्लिक यूआरएल के व्यवहार को ऑप्टिमाइज़ करना है या नहीं.
देखें कि
WKNavigationAction
ऑब्जेक्ट पर मौजूदnavigationType
प्रॉपर्टी, क्लिक टाइप है या नहीं. अगर हां, तो उसे ऑप्टिमाइज़ करें. कोड का उदाहरण,.linkActivated
की जांच करता है. यह सिर्फ़href
एट्रिब्यूट वाले लिंक पर क्लिक पर लागू होता है.WKNavigationAction
ऑब्जेक्ट परtargetFrame
प्रॉपर्टी देखें. अगर यहnil
दिखाता है, तो इसका मतलब है कि नेविगेशन का टारगेट एक नई विंडो है.WKWebView
उस क्लिक को मैनेज नहीं कर सकता, इसलिए इन क्लिक को मैन्युअल तरीके से मैनेज करना होगा.
यह तय करें कि यूआरएल को किसी बाहरी ब्राउज़र,
SFSafariViewController
या मौजूदा वेब व्यू में खोलना है या नहीं. कोड स्निपेट में,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://webview-api-for-ads-test.glitch.me#click-behavior-tests
को अपने वेब व्यू में जोड़ें. अलग-अलग तरह के लिंक पर क्लिक करके देखें कि वे आपके ऐप्लिकेशन में कैसे काम करते हैं.
यहां जांच करने योग्य कुछ बातें बताई गई हैं:
- हर लिंक, सही यूआरएल खोलता है.
- ऐप्लिकेशन पर वापस आने पर, जांच वाले पेज का काउंटर शून्य पर रीसेट नहीं होता, ताकि यह पुष्टि की जा सके कि पेज की स्थिति को बनाए रखा गया है.