研究了一下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 = truevar 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 AppTrackingTransparencyclass 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()// 延时一会执行,要不然可能拿不到广告IDFADispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [self] inif #available(iOS 14.0, *) {ATTrackingManager.requestTrackingAuthorization { [self] status inprint("请求idfa状态 \(status)")if status == .authorized {DAdHandler.init()print("idfa \(ASIdentifierManager.shared().advertisingIdentifier)")if self.splashAdInstance != nil {print("初始化广告位成功")}self.splashAdInstance?.delegate = selfself.splashAdInstance?.loadData()}}} else {DAdHandler.init()if self.splashAdInstance != nil {print("初始化广告位成功")}self.splashAdInstance?.delegate = selfself.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: Boolprivate var adInstanse: DSplashInstance?init(showAd: Binding<Bool>) {self._showAd = showAdadInstanse = DSplashInstance()adInstanse?.delegate = self}func closeAd() {showAd = false}var body: some View {VStack {Color.gray}}
}struct DSplashView_Previews: PreviewProvider {@State static var showAd: Bool = truestatic var previews: some View {DSplashView(showAd: $showAd)}
}

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


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

例子地址

SwiftUI接入穿山甲开屏广告相关推荐

  1. android接入admob开屏广告

    最近admob推出开屏广告.刚好公司也在做海外广告这块的业务.于是开始接入开屏广告,展示图如下: 个人感觉开屏广告比较像插屏广告.实现如下: 1.添加依赖; implementation 'com.g ...

  2. CocosCreator接入穿山甲广告(IOS)

    说明 接入穿山甲广告SDK 将Cocos Creator工程打包,在手机上运行,看能否正常运行游戏,保证游戏没有问题的前提下再引入SDK所需要的各种包 根据文档引入各种包,文档地址:https://p ...

  3. SwiftUI应用开屏广告界面项目(一)

    SwiftUI iOS应用开屏广告界面项目(一) 瞎扯 不想看我瞎扯可以直接跳到这 需求 源码 思路分析 页面跳转 计时 远程图片获取 瞎扯 最近开始玩Swift试试macOS和iOS开发 我都快忘了 ...

  4. 如何在我的应用启动界面实现「开屏广告」?

    什么是开屏广告 开屏广告是一种在应用启动时且在应用主界面显示之前需要被展示的广告.一般是5s展示时间,广告展示时间结束后自动进入应用,用户可以点击跳过按钮直接进入主界面. 开屏广告示例 开屏广告的优势 ...

  5. android多类型启动广告,如何在我的应用启动界面实现「开屏广告」?

    什么是开屏广告 开屏广告是一种在应用启动时且在应用主界面显示之前需要被展示的广告.一般是5s展示时间,广告展示时间结束后自动进入应用,用户可以点击跳过按钮直接进入主界面. 开屏广告示例 开屏广告的优势 ...

  6. Android接入穿山甲SDK并实现开屏广告

    一.穿山甲SDK的接入 0.进入官方下载地址,下载穿山甲AndroidSDK,下载的压缩包里包含.aar的sdk文件和工程demo. 1.将 SDK 压缩包内的 open_ad_sdk.aar 复制到 ...

  7. Android 接入穿山甲SDK之开屏广告

    大家可以先参考我的上一篇博客介绍了如何集成SDK以及一些工具类传送门 首先创建一个脚本写入如下内容 package com.unity3d.player.chuanshanjia;import and ...

  8. Unity接入穿山甲广告(使用Unity3D插件)

      最近要接入穿山甲广告,我把穿山甲广告相关的接入流程记录一下,说说最近踩过的坑,和怎么解决的问题.注意我们需求是只发安卓包没有iOS的需求,本篇只介绍Unity接入安卓相关的处理(帖子是2019年底 ...

  9. Unity接入穿山甲广告SDK(以及GroMoreDemo)

    Unity接入穿山甲广告SDK(以及GroMoreDemo) 下载安装 穿山甲官网SDK下载链接(https://www.csjplatform.com/union/media/union/downl ...

最新文章

  1. 5.8 程序示例--线性分类-机器学习笔记-斯坦福吴恩达教授
  2. php 折叠菜单,SlashdotMenu 折叠菜单
  3. Web Api 基于Zookeeper的服务注册与发现
  4. Cisco测试命令和TCP/IP连接故障处理
  5. CSS 3 中的伪类选择器
  6. php归档函数(按时间)实现
  7. IdentityServer4(10)- 添加对外部认证的支持之QQ登录
  8. 国内最全最详细的hadoop2.2.0集群的HA高可靠的最简单配置
  9. mdb 查询过于复杂_【律联云知产课堂】南京商标查询主要从哪些方面判断一个商标是否适合注册?...
  10. python 封装_Python面向对象之封装
  11. 香农编码的gui编码_香农编码
  12. 国产免费虚拟化OVM与 OpenStack对比
  13. MATLAB 解数独
  14. 上位机发送与接收下位机数据
  15. Groovy~Groovy的方法
  16. 如何在阿里云提交工单
  17. revit学习-视图
  18. python爬取今日头条后台数据_爬虫爬取今日头条数据代码实现
  19. 使用Save To Notion 保存豆瓣书籍或电影的惨痛教训/Save to Notion保存豆瓣电影不显示封面
  20. el-table 固定表头,固定列,动态高度,最简单的办法

热门文章

  1. 分分钟安装VMware,并安装linux操作系统
  2. macbook 打开以后没有声音,重启以后又好了的解决方法
  3. 苹果cms v10影视网站数据采集
  4. android国外壁纸app,国外Android精美主题、壁纸Ui界面设计大全-MyColorscreen
  5. 关于周报的写法和原则
  6. html怎么设置网页的大小怎么设置,css怎么设置字体大小
  7. 运维派 企业面试题3 为上题中的 十个随机字母_test.html 文件 更名
  8. Unity 2D教程 | 骨骼动画:创建动画
  9. linux电脑滚轮不能用,图文详解电脑鼠标滚轮不动了怎么办_电脑鼠标滚轮不能用的三种解决方法-系统城...
  10. 【.Net平台下插件开发】-MEF与MAF初步调研