如何获取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:
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
AdListenerto your BannerAd instance. - Override the
onAdClickedmethod 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:
- 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>
- Extract the URL from the intent in your WebView activity (same as the code above, but use
intent.data?.toString()instead of the extra).
For iOS, ad SDK delegates are your best bet. Using Google AdMob as an example:
- Implement the
GADBannerViewDelegateprotocol in your view controller. - 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) } } }
- 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) orWKNavigationDelegate(iOS) to ensure all subsequent links from the ad’s page stay within your app’s WebView.
内容的提问来源于stack exchange,提问作者ankushalg

