Skip to content

DeepAdx iOS(Swift)接入文档 ​

Swift 项目通过 CocoaPods 引入 DeepAdxSDK,使用桥接头文件或模块导入即可调用 SDK。


1. Podfile 配置 ​

ruby
platform :ios, '12.0'

source 'https://cdn.cocoapods.org/'

pod 'DeepAdxSDK', '2.0.6', :subspecs => ['Core', 'CSJ', 'GDT', 'KS', 'BD', 'GG', 'PAG']
bash
pod install --repo-update

Objective-C 集成细节见 iOS 接入文档。


2. Info.plist 配置 ​

按需添加以下配置(海外 Google 广告、ATT 等):

xml
<key>NSUserTrackingUsageDescription</key>
<string>用于向您提供更合适的广告内容</string>

<key>GADApplicationIdentifier</key>
<string>ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy</string>

Google 测试 ID 可向商务获取;生产环境请使用您自己的 AdMob Application ID。


3. 初始化 SDK ​

在用户同意隐私政策后初始化(示例放在 AppDelegate):

swift
import UIKit
import DeepAdxCore

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        setupDeepAdx()
        return true
    }

    func setupDeepAdx() {
        DeepAdxSdkConfig.shareInstance().level = .debug  // 上线请关闭 Debug
        let model = DeepAdxConfigModel()
        model.debugMode = false

        // App ID 从 DeepAdx 后台获取
        DeepAdxSdkConfig.shareInstance().registerAppID("YOUR_APP_ID", withConfig: model)
    }
}

测试用 App ID 与 placementId 见 示例测试 ID。官方 Swift Demo 支持按系统地区自动切换国内 / 海外 App ID,可参考 示例工程。


4. IDFA 授权(iOS 14+) ​

建议在 SceneDelegate 中请求 ATT 授权:

swift
import AppTrackingTransparency
import AdSupport

func requestTracking() {
    if #available(iOS 14, *) {
        ATTrackingManager.requestTrackingAuthorization { status in
            if status == .authorized {
                let idfa = ASIdentifierManager.shared().advertisingIdentifier.uuidString
                print("idfa = \(idfa)")
            }
        }
    }
}

5. 广告位示例 ​

接入时请始终传入 placementId(后台「广告单元」的 placement_id)。以下 YOUR_PLACEMENT_ID 请替换为实际 ID 或 测试 ID 中对应值。

swift
import DeepAdxCore
import DeepAdxAdspot

class BannerDemoVC: UIViewController, DeepAdxBannerDelegate {
    private var adBanner: DeepAdxBanner?

    func loadBanner() {
        let banner = DeepAdxBanner(adViewController: self, placementId: "YOUR_PLACEMENT_ID")
        banner.delegate = self
        adBanner = banner
        banner.loadAndShowAd()
    }
}

开屏 ​

swift
class SplashDemoVC: UIViewController, DeepAdxSplashDelegate {
    private var adSplash: DeepAdxSplash?

    func loadSplash() {
        let splash = DeepAdxSplash(viewController: self, placementId: "YOUR_PLACEMENT_ID")
        splash.delegate = self
        adSplash = splash
        splash.loadAndShowAd()
    }
}

插屏 ​

swift
class InterstitialDemoVC: UIViewController, DeepAdxInterstitialDelegate {
    private var adInterstitial: DeepAdxInterstitial?

    func loadInterstitial() {
        let ad = DeepAdxInterstitial(viewController: self, placementId: "YOUR_PLACEMENT_ID")
        ad.delegate = self
        adInterstitial = ad
        ad.loadAd()
    }

    func showInterstitial() {
        adInterstitial?.showAd()
    }
}

激励视频 ​

swift
class RewardDemoVC: UIViewController, DeepAdxRewardVideoDelegate {
    private var adReward: DeepAdxRewardVideo?

    func loadReward() {
        let ad = DeepAdxRewardVideo(viewController: self, placementId: "YOUR_PLACEMENT_ID")
        ad.delegate = self
        adReward = ad
        ad.loadAd()
    }

    func showReward() {
        adReward?.showAd()
    }
}

6. 示例工程 ​

完整 Swift Demo:Gitee DeepAdxSDK → Example/DeepAdxSwift。在线体验:蒲公英 deepadxsdk-ios(内测签名,需已登记设备)。

bash
cd Example
pod install
open DeepAdxDebug.xcworkspace   # 含 OC(DeepAdxDebug)与 Swift(DeepAdxSwift)两个 Demo

DeepAdx SDK 官方文档