WebView क्लिक व्यवहार ऑप्टिमाइज़ करें

अगर आपका ऐप्लिकेशन वेब कॉन्टेंट दिखाने के लिए WebView का इस्तेमाल करता है, तो आपको इन वजहों से क्लिक व्यवहार को ऑप्टिमाइज़ करने पर विचार करना चाहिए:

  • WebView, कस्टम यूआरएल स्कीम के साथ काम नहीं करता. अगर क्लिक डेस्टिनेशन किसी दूसरे ऐप्लिकेशन पर है, तो विज्ञापनों में ये स्कीम दिख सकती हैं. उदाहरण के लिए, Google Play का क्लिक मिलने पर खुलने वाला यूआरएल, market:// का इस्तेमाल कर सकता है.

इस गाइड में, वेब व्यू के कॉन्टेंट को बनाए रखते हुए, मोबाइल वेब व्यू में क्लिक व्यवहार को ऑप्टिमाइज़ करने के लिए सुझाए गए तरीके बताए गए हैं.

ज़रूरी शर्तें

लागू करना

अपने WebView इंस्टेंस में क्लिक व्यवहार को ऑप्टिमाइज़ करने के लिए, यह तरीका अपनाएं:

  1. WebViewClient पर जाकर, shouldOverrideUrlLoading() को बदलें. इस तरीके को तब कॉल किया जाता है, जब मौजूदा WebView में कोई यूआरएल लोड होने वाला हो.

  2. यह तय करें कि क्लिक यूआरएल के व्यवहार को बदलना है या नहीं.

    कोड का उदाहरण यह जांच करता है कि मौजूदा डोमेन, टारगेट डोमेन से अलग है या नहीं. यह सिर्फ़ एक तरीका है, क्योंकि इस्तेमाल की जाने वाली शर्तें अलग-अलग हो सकती हैं.

  3. यह तय करें कि यूआरएल को किसी बाहरी ब्राउज़र, Android कस्टम टैब या मौजूदा वेब व्यू में खोलना है या नहीं. इस गाइड में, Android कस्टम टैब लॉन्च करके, साइट से बाहर निकलने के लिए यूआरएल खोलने का तरीका बताया गया है.

कोड का उदाहरण

सबसे पहले, अपने मॉड्यूल-लेवल की build.gradle फ़ाइल में androidx.browser डिपेंडेंसी जोड़ें. आम तौर पर, यह फ़ाइल app/build.gradle होती है. कस्टम टैब के लिए यह ज़रूरी है:

dependencies {
  implementation 'androidx.browser:browser:1.5.0'
}

नीचे दिए गए कोड स्निपेट में, shouldOverrideUrlLoading() को लागू करने का तरीका बताया गया है:

Java

public class MainActivity extends AppCompatActivity {

  private WebView webView;

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // ... Register the WebView.

    webView = new WebView(this);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.setWebViewClient(
        new WebViewClient() {
          // 1. Implement the web view click handler.
          @Override
          public boolean shouldOverrideUrlLoading(
              WebView view,
              WebResourceRequest request) {
            // 2. Determine whether to override the behavior of the URL.
            // If the target URL has no host, return early.
            if (request.getUrl().getHost() == null) {
              return false;
            }

            // Handle custom URL schemes such as market:// by attempting to
            // launch the corresponding application in a new intent.
            if (!request.getUrl().getScheme().equals("http")
                && !request.getUrl().getScheme().equals("https")) {
              Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
              // If the URL cannot be opened, return early.
              try {
                MainActivity.this.startActivity(intent);
              } catch (ActivityNotFoundException exception) {
                Log.d("TAG", "Failed to load URL with scheme:" + request.getUrl().getScheme());
              }
              return true;
            }

            String currentDomain;
            // If the current URL's host cannot be found, return early.
            try {
              currentDomain = new URI(view.getUrl()).toURL().getHost();
            } catch (URISyntaxException | MalformedURLException exception) {
              // Malformed URL.
              return false;
            }
            String targetDomain = request.getUrl().getHost();

            // If the current domain equals the target domain, the
            // assumption is the user is not navigating away from
            // the site. Reload the URL within the existing web view.
            if (currentDomain.equals(targetDomain)) {
              return false;
            }

            // 3. User is navigating away from the site, open the URL in
            // Custom Tabs to preserve the state of the web view.
            CustomTabsIntent intent = new CustomTabsIntent.Builder().build();
            intent.launchUrl(MainActivity.this, request.getUrl());
            return true;
          }
        });
  }
}

Kotlin

class MainActivity : AppCompatActivity() {

  private lateinit var webView: WebView

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // ... Register the WebView.

    webView.webViewClient = object : WebViewClient() {
      // 1. Implement the web view click handler.
      override fun shouldOverrideUrlLoading(
          view: WebView?,
          request: WebResourceRequest?
      ): Boolean {
        // 2. Determine whether to override the behavior of the URL.
        // If the target URL has no host, return early.
        request?.url?.host?.let { targetDomain ->
          val currentDomain = URI(view?.url).toURL().host

          // Handle custom URL schemes such as market:// by attempting to
          // launch the corresponding application in a new intent.
          if (!request.url.scheme.equals("http") &&
              !request.url.scheme.equals("https")) {
            val intent = Intent(Intent.ACTION_VIEW, request.url)
            // If the URL cannot be opened, return early.
            try {
              this@MainActivity.startActivity(intent)
            } catch (exception: ActivityNotFoundException) {
              Log.d("TAG", "Failed to load URL with scheme: ${request.url.scheme}")
            }
            return true
          }

          // If the current domain equals the target domain, the
          // assumption is the user is not navigating away from
          // the site. Reload the URL within the existing web view.
          if (currentDomain.equals(targetDomain)) {
            return false
          }

          // 3. User is navigating away from the site, open the URL in
          // Custom Tabs to preserve the state of the web view.
          val customTabsIntent = CustomTabsIntent.Builder().build()
          customTabsIntent.launchUrl(this@MainActivity, request.url)
          return true
        }
        return false
      }
    }
  }
}

अपने पेज के नेविगेशन की जांच करना

पेज के नेविगेशन में किए गए बदलावों की जांच करने के लिए,

https://webview-api-for-ads-test.glitch.me#click-behavior-tests

को अपने वेब व्यू में जोड़ें. अलग-अलग तरह के लिंक पर क्लिक करके देखें कि वे आपके ऐप्लिकेशन में कैसे काम करते हैं.

यहां जांच करने योग्य कुछ बातें बताई गई हैं:

  • हर लिंक, उस यूआरएल को खोलता है जिसे खोलना है.
  • ऐप्लिकेशन पर वापस आने पर, जांच वाले पेज का काउंटर शून्य पर रीसेट नहीं होता, ताकि यह पुष्टि की जा सके कि पेज की स्थिति को बनाए रखा गया है.