从零集成腾讯广告SDK

这里写自定义目录标题

从零集成腾讯广告SDK 话不多说直接干: 在我们工程的里新增

pod 'GDTMobSDK'

在我们的项目里新增一个oc的.h文件用于 需要用到的文件:

#import "GDTUnifiedInterstitialAd.h"
#import "GDTSplashAd.h"
#import "GDTSDKConfig.h"

3. 新增文件:,这里我们用了两种广告类型(1.优量汇插屏, 2.优量汇开屏)复制代码:

import UIKit
//腾讯广告
class AdManagerGDTAdSplash: NSObject {
    private var splashAd: GDTSplashAd!
    var placementId: String?
    var success: (() -> Void)?
    var failure: (() -> Void)?
    
    init(_ placementId: String) {
        GDTSDKConfig.registerAppId("xxxxxxx")//申请的appid
        self.placementId = placementId
    }
    func showSplashAd(success: (() -> Void)? = nil, failure: (() -> Void)? = nil) {
        guard let placementId = self.placementId  else { return }
        self.success = success
        self.failure = failure
        splashAd =  GDTSplashAd.init(placementId: placementId)
        splashAd.delegate = self
        splashAd.fetchDelay = 3
        splashAd.load()
    }
}
extension AdManagerGDTAdSplash: GDTSplashAdDelegate {
//    MARK:GDTSplashAdDelegate
    func splashAdDidLoad(_ splashAd: GDTSplashAd!) {
        var window = UIApplication.shared.keyWindow
        
        if !H.isIOS12AndLow() {
            window = UIApplication.shared.windows.first
        }
        
        if splashAd.isAdValid() {
            splashAd.show(in: window, withBottomView: nil, skip: nil)
        }
    }
    func splashAdSuccessPresentScreen(_ splashAd: GDTSplashAd!) {
        print(#function)
    }
    
    func splashAdFail(toPresent splashAd: GDTSplashAd!, withError error: Error!) {
        print(#function,error ?? "")
        self.splashAd = nil
        self.failure?()
    }
    
    func splashAdExposured(_ splashAd: GDTSplashAd!) {
        print(#function)
    }
    
    func splashAdClicked(_ splashAd: GDTSplashAd!) {
        print(#function)
    }
    
    func splashAdApplicationWillEnterBackground(_ splashAd: GDTSplashAd!) {
        print(#function)
    }
    
    func splashAdWillClosed(_ splashAd: GDTSplashAd!) {
        print(#function)
    }
    
    func splashAdClosed(_ splashAd: GDTSplashAd!) {
        print(#function)
        self.splashAd = nil
        self.success?()
    }
    
    func splashAdDidPresentFullScreenModal(_ splashAd: GDTSplashAd!) {
        print(#function)
    }
    
    func splashAdWillDismissFullScreenModal(_ splashAd: GDTSplashAd!) {
        print(#function)
    }
    
    func splashAdDidDismissFullScreenModal(_ splashAd: GDTSplashAd!) {
        print(#function)
    }
}
class AdManagerGDTAd: NSObject {
    private var interstitial: GDTUnifiedInterstitialAd!
    var placementId: String?
    var success: (() -> Void)?
    var failure: (() -> Void)?
    init(_ placementId: String) {
        GDTSDKConfig.registerAppId("1111398218")
        self.placementId = placementId
    }
    
    func showInterstital(success: @escaping(() -> Void), failure: @escaping(() -> Void)) -> GDTUnifiedInterstitialAd {
        guard let placementId = self.placementId else { return self.interstitial }
        self.success = success
        
        if ((self.interstitial) != nil) {
            self.interstitial.delegate = nil
        }
        self.interstitial = GDTUnifiedInterstitialAd.init(placementId: placementId)
        self.interstitial.delegate = self
        self.interstitial.videoMuted = true // 设置视频是否Mute
        self.interstitial.loadFullScreenAd()// 加载插屏全屏视频广告
        return self.interstitial
    }
}
extension AdManagerGDTAd: GDTUnifiedInterstitialAdDelegate {
    func unifiedInterstitialSuccess(toLoad unifiedInterstitial: GDTUnifiedInterstitialAd) {
        
    }
    
    func unifiedInterstitialClicked(_ unifiedInterstitial: GDTUnifiedInterstitialAd) {
        
    }
    func unifiedInterstitialWillExposure(_ unifiedInterstitial: GDTUnifiedInterstitialAd) {
        
    }
    func unifiedInterstitialDidPresentScreen(_ unifiedInterstitial: GDTUnifiedInterstitialAd) {
        
    }
    func unifiedInterstitialDidDismissScreen(_ unifiedInterstitial: GDTUnifiedInterstitialAd) {
        self.success?()
    }
    func unifiedInterstitialAdDidPresentFullScreenModal(_ unifiedInterstitial: GDTUnifiedInterstitialAd) {
    }
    func unifiedInterstitialWillPresentScreen(_ unifiedInterstitial: GDTUnifiedInterstitialAd) {
        
    }
    func unifiedInterstitialWillLeaveApplication(_ unifiedInterstitial: GDTUnifiedInterstitialAd) {
        
    }
    func unifiedInterstitialFail(toLoad unifiedInterstitial: GDTUnifiedInterstitialAd, error: Error) {
        print("xxxxx=toload",error)
    }
    func unifiedInterstitialFail(toPresent unifiedInterstitial: GDTUnifiedInterstitialAd, error: Error) {
        print("xxxxx=topresent",error)
    }
    func unifiedInterstitialAdDidDismissFullScreenModal(_ unifiedInterstitial: GDTUnifiedInterstitialAd) {
        
    }
    func unifiedInterstitialAdWillPresentFullScreenModal(_ unifiedInterstitial: GDTUnifiedInterstitialAd) {
        
    }
    func unifiedInterstitialAdWillDismissFullScreenModal(_ unifiedInterstitial: GDTUnifiedInterstitialAd) {
       
    }
    func unifiedInterstitialAdViewDidPresentVideoVC(_ unifiedInterstitial: GDTUnifiedInterstitialAd) {
        
    }
    func unifiedInterstitialAdViewDidDismissVideoVC(_ unifiedInterstitial: GDTUnifiedInterstitialAd) {
        
    }
    func unifiedInterstitialAdViewWillPresentVideoVC(_ unifiedInterstitial: GDTUnifiedInterstitialAd) {
        
    }
    func unifiedInterstitialAdViewWillDismissVideoVC(_ unifiedInterstitial: GDTUnifiedInterstitialAd) {
        
    }
    func unifiedInterstitialAd(_ unifiedInterstitial: GDTUnifiedInterstitialAd, playerStatusChanged status: GDTMediaPlayerStatus) {
    }
}

如果是只集成了一种类型的广告,比如开屏,那我们就可以不用初始化,直接让上面的代码的等于你申请的例如""

 init(_ placementId: String) {
       GDTSDKConfig.registerAppId("xxxxxxx")//申请的appid
       self.placementId = "xxxxxx" // 申请的placementId
   }

如果是不同类型的话,或者调用多次需要不同广告的话,我们需要在里初始化的时候输入一下代码:

// 优量汇插屏
   let adManagerGDTAdCalc = AdManagerGDTAd.init("7091772179125733")
   let adManagerGDTAdDetail = AdManagerGDTAd.init("4011278119295553")
   // 优量汇开屏
   let adManagerGDTAdSplashCalc = AdManagerGDTAdSplash("6021872260330766")
   let adManagerGDTAdSplashDetail = AdManagerGDTAdSplash("2041275240436711")

在你想让他点击某一个按钮的时候或者启动页面的时候展示广告,输入一下代码调用广告(这里我们判断广告是否成功,所以用了,成功或者失败):

adManagerGDTAdSplashCalc.showSplashAd {} failure: {}
adManagerGDTAdSplashDetail.showSplashAd() {self.toDetail()} failure: {self.toDetail()}

大功告成看看效果:

附上优量汇集成教程的网址:

云衔科技是一家专注于企业数字化广告营销解决方案的服务商。公司凭借深厚的行业经验和专业技术能力,致力于为企业客户提供全方位、更高效的数字化广告营销与运营服务。

发表回复