iOS Swift应用首页启动时AdMob Banner不显示问题求助
Hey there! I know it’s frustrating when your AdMob Banner won’t show up on the home screen even after you’ve double-checked your code logic. Let’s walk through the most likely fixes to get it displaying on first launch:
Verify AdMob Account & Unit Setup
- Double-check that you’ve created a valid Banner ad unit ID in your AdMob dashboard—typos here are super common! For testing, use AdMob’s official test ID:
ca-app-pub-3940256099942544/2934735716(this guarantees ads will load, ruling out real-world fill issues). - Ensure your app’s GADApplicationIdentifier is correctly added to
Info.plistunder theGADApplicationIdentifierkey. Missing this will break all AdMob functionality.
- Double-check that you’ve created a valid Banner ad unit ID in your AdMob dashboard—typos here are super common! For testing, use AdMob’s official test ID:
Check Layout Constraints
- Even if your code is right, incorrect constraints might hide the banner off-screen. Make sure you’re pinning the banner to the top of the safe area with leading/trailing constraints that match the screen width:
bannerView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ bannerView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor), bannerView.leadingAnchor.constraint(equalTo: view.leadingAnchor), bannerView.trailingAnchor.constraint(equalTo: view.trailingAnchor) ]) - Confirm the banner uses a valid AdMob ad size (e.g.,
GADAdSizeBannerfor standard 50pt height on iPhones). Using a custom size that’s too small might prevent ads from loading.
- Even if your code is right, incorrect constraints might hide the banner off-screen. Make sure you’re pinning the banner to the top of the safe area with leading/trailing constraints that match the screen width:
Validate Initialization & Loading Timing
- Initialize AdMob early in your app’s lifecycle—add this in
AppDelegate’sdidFinishLaunchingWithOptions:GADMobileAds.sharedInstance().start(completionHandler: nil) - Load the banner in
viewDidLoadorviewWillAppearof your home view controller, and make sure you’ve set therootViewControllerproperty on theGADBannerView(this is required for ad interactions).
- Initialize AdMob early in your app’s lifecycle—add this in
Handle Privacy Requirements (iOS 14+)
- For iOS 14 and above, you need to include the
NSUserTrackingUsageDescriptionkey inInfo.plistto explain why you need user tracking. Without this, AdMob can’t access the IDFA, which reduces ad fill rates. - Don’t forget to request user tracking authorization using the App Tracking Transparency framework before loading ads:
import AppTrackingTransparency import AdSupport func requestTrackingAuthorization() { ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in // Load ads after authorization is granted (or denied—ads might still load but with limited targeting) DispatchQueue.main.async { self.bannerView.load(GADRequest()) } }) }
- For iOS 14 and above, you need to include the
Check Xcode Console for Errors
- This is the most critical step! Launch your app and look for AdMob-related logs in the Xcode console. Common errors include:
Ad failed to load: Request Error: No ad to show.(No fill for your ad unit—use the test ID to confirm)GADRequestError Code: 2(Invalid ad unit ID or app ID)GADRequestError Code: 3(Network issue or ad filtering)
- Implement the
GADBannerViewDelegateto catch failures explicitly:func adViewDidFailToReceiveAdWithError(_ bannerView: GADBannerView, error: Error) { print("Ad load failed: \(error.localizedDescription)") if let gadError = error as? GADRequestError { print("AdMob Error Code: \(gadError.code)") } }
- This is the most critical step! Launch your app and look for AdMob-related logs in the Xcode console. Common errors include:
Start with testing the official AdMob test ID—if that loads, the issue is with your real ad unit (e.g., pending approval, low fill rate). If the test ID still doesn’t show, dive into constraints, initialization timing, and privacy settings next.
内容的提问来源于stack exchange,提问作者Christoffer Karlsson
相关产品推荐
相关产品推荐

