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

iOS应用集成Admob:能否统一配置Admob ID实现复用?

Yes, you absolutely can centralize your AdMob IDs in Xcode!

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

  1. Open your project's Info.plist file.
  2. Right-click and select Add Row.
  3. 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

  1. Add new .xcconfig files (e.g., Debug.xcconfig and Release.xcconfig).
  2. 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
  1. Go to your project settings → Info tab.
  2. Under Configurations, assign Debug.xcconfig to the Debug configuration and Release.xcconfig to Release.

Step 3: Reference Variables in Info.plist

In Info.plist, replace the static ID values with the configuration variables:

  • For the AdmobBannerID key, 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 .xcconfig or Info.plist (consider using environment variables or a secrets manager for sensitive data).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 06:47:07