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

अगर आपका Android ऐप्लिकेशन, वेब कॉन्टेंट दिखाने 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 URL(view.getUrl()).getHost();
            } catch (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 = URL(view?.url).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

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

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

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