iOS应用集成Admob:能否统一配置Admob ID实现复用?
Just like Android Studio's strings.xml, there are several clean ways to store your AdMob IDs in a single place and reference them anywhere in your code—no more copying and pasting IDs every time you create an ad. Here are the most common approaches:
1. Use a Constants Struct (Simplest for Basic Needs)
Create a dedicated Swift struct to hold all your AdMob IDs as static properties. This keeps everything organized in one file and makes updates a breeze.
Step 1: Create the Constants File
Add a new Swift file (e.g., AdmobConstants.swift) with this code:
import Foundation struct AdmobConstants { // Replace these with your actual AdMob IDs static let bannerAdUnitID = "ca-app-pub-xxxxxxxxxxxxxxxx/yyyyyyyyyy" static let interstitialAdUnitID = "ca-app-pub-xxxxxxxxxxxxxxxx/zzzzzzzzzz" static let rewardedAdUnitID = "ca-app-pub-xxxxxxxxxxxxxxxx/aaaaaaaaaa" // Optional: Include test IDs for development static let testBannerID = "ca-app-pub-3940256099942544/2934735716" }
Step 2: Reference the IDs Anywhere
Whenever you need to create an ad, just use the static property:
// Example: Initializing a banner ad let bannerView = GADBannerView(adUnitID: AdmobConstants.bannerAdUnitID)
2. Store IDs in Info.plist (Better for Environment Flexibility)
Storing IDs in Info.plist lets you separate configuration from code, and it's easier to switch between development and production IDs if you use build configurations.
Step 1: Add IDs to Info.plist
- Open your project's
Info.plistfile. - Right-click and select Add Row.
- For each AdMob ID, add a key (e.g.,
AdmobBannerID) and set its type to String, then paste your ID as the value.
Step 2: Create a Helper Struct to Fetch IDs
To avoid repeating boilerplate code every time you access the IDs, wrap the fetch logic in a struct:
import Foundation struct AdmobConfig { static var bannerAdUnitID: String { guard let id = Bundle.main.object(forInfoDictionaryKey: "AdmobBannerID") as? String else { fatalError("AdMob Banner ID is missing from Info.plist!") } return id } static var interstitialAdUnitID: String { guard let id = Bundle.main.object(forInfoDictionaryKey: "AdmobInterstitialID") as? String else { fatalError("AdMob Interstitial ID is missing from Info.plist!") } return id } }
Step 3: Use the Helper Struct
Now you can reference the IDs cleanly:
// Example: Loading an interstitial ad GADInterstitialAd.load( withAdUnitID: AdmobConfig.interstitialAdUnitID, request: GADRequest() ) { ad, error in // Handle ad load result }
3. Use .xcconfig Files (Best for Multiple Environments)
If you need different AdMob IDs for development, staging, and production, .xcconfig files are the way to go. They let you define environment-specific values without changing your code or Info.plist.
Step 1: Create Configuration Files
- Add new
.xcconfigfiles (e.g.,Debug.xcconfigandRelease.xcconfig). - In each file, define your AdMob IDs:
// Debug.xcconfig ADMOB_BANNER_ID = "ca-app-pub-3940256099942544/2934735716" // Test ID ADMOB_INTERSTITIAL_ID = "ca-app-pub-3940256099942544/4411468910" // Test ID // Release.xcconfig ADMOB_BANNER_ID = "ca-app-pub-xxxxxxxxxxxxxxxx/yyyyyyyyyy" // Production ID ADMOB_INTERSTITIAL_ID = "ca-app-pub-xxxxxxxxxxxxxxxx/zzzzzzzzzz" // Production ID
Step 2: Link Config Files to Build Configurations
- Go to your project settings → Info tab.
- Under Configurations, assign
Debug.xcconfigto the Debug configuration andRelease.xcconfigto Release.
Step 3: Reference Variables in Info.plist
In Info.plist, replace the static ID values with the configuration variables:
- For the
AdmobBannerIDkey, set the value to$(ADMOB_BANNER_ID) - Do the same for other IDs using their respective variables.
Step 4: Access IDs as Before
Use the same AdmobConfig struct from method 2—no changes needed! The correct ID will be loaded based on your build configuration.
Pro Tips
- Always use AdMob's official test IDs during development to avoid violating AdMob policies.
- Keep your production IDs secure: never commit them to public repositories if you're using
.xcconfigor Info.plist (consider using environment variables or a secrets manager for sensitive data).
内容的提问来源于stack exchange,提问作者makkhokher

