You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取Banner广告点击后的URL并在App内置WebView打开

Hey there! Let’s break down how to intercept banner ad clicks and load their URLs in your app’s built-in WebView instead of the default browser—this is totally achievable, though the steps vary a bit between Android and iOS. Here’s what you need to do:

Android Implementation

If you’re using a common ad SDK like Google AdMob, here are two reliable approaches:

Approach 1: Use Ad SDK Callback to Capture URL

Many ad SDKs let you hook into click callbacks and extract the target URL directly. For AdMob, here’s how:

  • Attach a custom AdListener to your BannerAd instance.
  • Override the onAdClicked method to grab the click URL from the ad’s metadata, then launch your WebView activity with that URL.
// In your activity/fragment where the banner is initialized
bannerAd.adListener = object : AdListener() {
    override fun onAdClicked() {
        super.onAdClicked()
        bannerAd.adInfo?.clickUrl?.let { adUrl ->
            // Launch your custom WebView activity
            val intent = Intent(this@YourActivity, CustomWebViewActivity::class.java)
            intent.putExtra("AD_TARGET_URL", adUrl)
            startActivity(intent)
        }
    }
}

Then in your CustomWebViewActivity:

class CustomWebViewActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_web_view)
        
        val webView = findViewById<WebView>(R.id.your_webview)
        // Configure WebView to stay within the app
        webView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
                view?.loadUrl(request?.url.toString())
                return true
            }
        }
        
        // Load the intercepted ad URL
        intent.getStringExtra("AD_TARGET_URL")?.let { url ->
            webView.loadUrl(url)
        }
    }
}

Approach 2: Intercept Browser Intent

If your ad SDK doesn’t expose the click URL directly, you can intercept the ACTION_VIEW intent that would normally launch the default browser:

  1. Add an intent filter to your WebView activity in AndroidManifest.xml:
<activity android:name=".CustomWebViewActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="http" />
        <data android:scheme="https" />
    </intent-filter>
</activity>
  1. Extract the URL from the intent in your WebView activity (same as the code above, but use intent.data?.toString() instead of the extra).
iOS Implementation

For iOS, ad SDK delegates are your best bet. Using Google AdMob as an example:

  1. Implement the GADBannerViewDelegate protocol in your view controller.
  2. Override bannerView(_:shouldOpenInApplication:) to block the default browser launch, then grab the click URL and load it into your WebView.
class YourViewController: UIViewController, GADBannerViewDelegate {
    private var bannerView: GADBannerView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Initialize your banner ad
        bannerView = GADBannerView(adSize: GADAdSizeBanner)
        bannerView.adUnitID = "YOUR_AD_UNIT_ID"
        bannerView.rootViewController = self
        bannerView.delegate = self
        view.addSubview(bannerView)
        bannerView.load(GADRequest())
    }

    // Prevent ad from opening default browser
    func bannerView(_ bannerView: GADBannerView, shouldOpenInApplication application: UIApplication?) -> Bool {
        return false
    }

    // Capture the URL and load into WebView
    func bannerView(_ bannerView: GADBannerView, didOpenApplication application: UIApplication?) {
        if let clickUrl = bannerView.adInfo?.clickUrl {
            let webVC = WebViewController()
            webVC.targetUrl = clickUrl
            present(webVC, animated: true)
        }
    }
}

In your WebViewController, configure a WKWebView to load the URL:

class WebViewController: UIViewController {
    var targetUrl: URL?
    private let webView = WKWebView()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(webView)
        webView.frame = view.bounds
        webView.navigationDelegate = self
        targetUrl.map { webView.load(URLRequest(url: $0)) }
    }
}

extension WebViewController: WKNavigationDelegate {
    // Ensure redirects stay in the app's WebView
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if navigationAction.navigationType == .linkActivated {
            webView.load(navigationAction.request)
            decisionHandler(.cancel)
        } else {
            decisionHandler(.allow)
        }
    }
}
Key Notes to Remember
  • Check Your Ad SDK Docs: Every ad network (Facebook Audience Network, Unity Ads, etc.) has its own callback methods. Always start with their official documentation—they might have a dedicated way to customize click behavior.
  • Compliance Matters: Make sure your implementation doesn’t violate the ad network’s policies. Some networks require proper tracking of clicks, so avoid bypassing their built-in mechanisms entirely.
  • WebView Configuration: Always set a custom WebViewClient (Android) or WKNavigationDelegate (iOS) to ensure all subsequent links from the ad’s page stay within your app’s WebView.

内容的提问来源于stack exchange,提问作者ankushalg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:44:12