美文网首页
SwiftUI接入穿山甲广告

SwiftUI接入穿山甲广告

作者: xo1988 | 来源:发表于2022-08-29 12:04 被阅读0次

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

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

    1. 首先要先注册一个账号,穿山甲地址

    1. 在广告变现->流量->应用中创建一个应用并记录下应用ID。 11.png
    1. 在广告变现->流量->代码位创建一个代码位并记录下代码位ID。注意要把是否应用于GroMore选择成否,要不然就需要用GreMore引擎

      22.png
    2. 记录账号ID。 33.png

    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. 测试的话,需要用到的增加测试工具项

    44.png

    IDFA如果同意的话,在日志中就能看到了。

    例子地址

    相关文章

      网友评论

          本文标题:SwiftUI接入穿山甲广告

          本文链接:https://www.haomeiwen.com/subject/wqwhnrtx.html