SwiftUI接入穿山甲开屏广告

研究了一下SwiftUI怎么接入穿山甲。

基于穿山甲sdk版本 4.7.0.8 例子地址

1. 首先要先注册一个账号,穿山甲地址
  1. 在广告变现->流量->应用中创建一个应用并记录下应用ID。在这里插入图片描述

  2. 在广告变现->流量->代码位创建一个代码位并记录下代码位ID。注意要把是否应用于GroMore选择成否,要不然就需要用GreMore引擎在这里插入图片描述

  3. 记录账号ID。在这里插入图片描述

2. 安排广告显示位置
import SwiftUI

@main
struct DemoApp: App {
    // 控制广告是否显示
    @State var showAd: Bool = true
    
    var body: some Scene {
        WindowGroup {
            if showAd == true {
                DSplashView(showAd: $showAd)
            } else {
                ContentView()
            }
        }
    }
}

// 获取根rootViewController
extension UIApplication {
  var currentKeyWindow: UIWindow? {
    UIApplication.shared.connectedScenes
      .filter { $0.activationState == .foregroundActive }
      .map { $0 as? UIWindowScene }
      .compactMap { $0 }
      .first?.windows
      .filter { $0.isKeyWindow }
      .first
  }

  var rootViewController: UIViewController? {
    currentKeyWindow?.rootViewController
  }
}

2.广告初始化,由于高版本的直接使用switf初始化有点问题,改成包了一个OC

DAdHandler.h

#ifndef DAdHandler_h
#define DAdHandler_h

#import <Foundation/Foundation.h>

@interface DAdHandler : NSObject

- (instancetype)init;

@end

#endif /* Header_h */

DAdHandler.m

#import <Foundation/Foundation.h>
#import "DAdHandler.h"
#import <BUAdSDK/BUAdSDKManager.h>
#import <BUAdSDK/BUAdSDKConfiguration.h>

@interface DAdHandler()

@end

@implementation DAdHandler

- (instancetype)init {
    if (self = [super init]) {
        [self initADSdk];
    }
    return self;
}

- (void)initADSdk {
    BUAdSDKConfiguration* config = [BUAdSDKConfiguration alloc];
    [config setAppID:@"5328067"];
    
    NSLog(@"DAdHandler init");
    
    [BUAdSDKManager startWithSyncCompletionHandler:^(BOOL success, NSError *error) {
        if (success) {
            NSLog(@"初始化成功");
        } else {
            NSLog(@"初始化失败");
        }
    }];
}

@end
3. 广告界面
//
//  DSplashView.swift
//  Demo
//
//  Created by wangyu on 2022/8/25.
//

import SwiftUI
import BUAdSDK
import AdSupport
import AppTrackingTransparency

class DSplashInstance: NSObject, BUSplashAdDelegate {
    var delegate: DSplashInstanceProtocol?
    
    lazy var splashAdInstance: BUSplashAd? = {
        let instance = BUSplashAd.init(slotID: "887901739", adSize: .init(width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
        return instance
    }()
    
     override init() {
         super.init()
        
         // 延时一会执行,要不然可能拿不到广告IDFA
         DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [self] in
             if #available(iOS 14.0, *) {
                 ATTrackingManager.requestTrackingAuthorization { [self] status in
                     print("请求idfa状态 \(status)")
                     if status == .authorized {
                         DAdHandler.init()
                         print("idfa \(ASIdentifierManager.shared().advertisingIdentifier)")
                         
                         

                         if self.splashAdInstance != nil {
                             print("初始化广告位成功")
                         }

                         self.splashAdInstance?.delegate = self
                         self.splashAdInstance?.loadData()
                     }
                 }
             } else {
                 DAdHandler.init()

                 if self.splashAdInstance != nil {
                     print("初始化广告位成功")
                 }

                 self.splashAdInstance?.delegate = self
                 self.splashAdInstance?.loadData()
             }
         }
    }
    
    func splashAdLoadSuccess(_ splashAd: BUSplashAd) {
        print("广告加载成功")
    }
    
    func splashAdLoadFail(_ splashAd: BUSplashAd, error: BUAdError?) {
        print("广告加载失败 \(error?.userInfo)")
    }
    
    func splashAdRenderSuccess(_ splashAd: BUSplashAd) {
        print("广告加载渲染成功")
        if UIApplication.shared.rootViewController != nil {
            splashAd.showSplashView(inRootViewController: UIApplication.shared.rootViewController!)
        }
    }
    
    func splashAdRenderFail(_ splashAd: BUSplashAd, error: BUAdError?) {
        print("广告加载渲染失败")
    }
    
    func splashAdWillShow(_ splashAd: BUSplashAd) {
        print("广告将要显示")
    }
    
    func splashAdDidShow(_ splashAd: BUSplashAd) {
        print("广告已经显示")
    }
    
    func splashAdViewControllerDidClose(_ splashAd: BUSplashAd) {
        print("广告关闭")
        delegate?.closeAd()
    }
    
    func splashDidCloseOtherController(_ splashAd: BUSplashAd, interactionType: BUInteractionType) {
        print("广告关闭其他")
    }
    
    func splashVideoAdDidPlayFinish(_ splashAd: BUSplashAd, didFailWithError error: Error) {
        print("广告播放完成")
    }
    
    func splashAdDidClick(_ splashAd: BUSplashAd) {
        print("广告点击")
    }
    
    func splashAdDidClose(_ splashAd: BUSplashAd, closeType: BUSplashAdCloseType) {
        print("广告关闭")
    }
}

protocol DSplashInstanceProtocol {
    func closeAd()
}

struct DSplashView: View, DSplashInstanceProtocol {
    @Binding var showAd: Bool
    private var adInstanse: DSplashInstance?
    
    init(showAd: Binding<Bool>) {
        self._showAd = showAd
        adInstanse = DSplashInstance()
        adInstanse?.delegate = self
    }
    
    func closeAd() {
        showAd = false
    }
    
    var body: some View {
        VStack {
            Color.gray
        }
    }
}

struct DSplashView_Previews: PreviewProvider {
    @State static var showAd: Bool = true
    
    static var previews: some View {
        DSplashView(showAd: $showAd)
    }
}

5. 测试的话,需要用到的增加测试工具项

在这里插入图片描述
IDFA如果同意的话,在日志中就能看到了。

例子地址

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xyccstudio

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值