android接入admob开屏广告

最近admob推出开屏广告、刚好公司也在做海外广告这块的业务。于是开始接入开屏广告,展示图如下:

个人感觉开屏广告比较像插屏广告、实现如下:

1、添加依赖;

 implementation 'com.google.firebase:firebase-ads:19.4.0'
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
    annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"

 2、AndroidManifest.xml

<meta-data
    android:name="com.google.android.gms.ads.APPLICATION_ID"
    android:value="ca-app-pub-3940256099942544~3347511713" />

3、关于admob测试广告位:

  String test_appid = "ca-app-pub-3940256099942544~3347511713";
    String test_interId = "ca-app-pub-3940256099942544/1033173712";
    String test_banner = "ca-app-pub-3940256099942544/6300978111";
    String test_native = "ca-app-pub-3940256099942544/2247696110";
    String test_open = "ca-app-pub-3940256099942544/3419835294";

4、Application添加如下代码:

 private static AppOpenManager appOpenManager;
  @Override
    public void onCreate() {
        super.onCreate();
      initOPenScreen();
    }
 private void initOPenScreen() {
        MobileAds.initialize(
                this,
                initializationStatus 
### iOS 应用中接入 AdMob 开屏广告 #### 准备工作 为了在 iOS 应用程序中集成 AdMob开屏广告 (Splash Ads),开发者需先完成一些准备工作。确保已安装最新版本的 Xcode 和 CocoaPods 工具,并注册成为 Google AdMob 发布商账户,获取到用于配置项目的 App ID。 #### 配置 Podfile 文件 通过 CocoaPods 来管理依赖库是最简便的方式之一。编辑 `Podfile` 文件,在目标项目下添加如下行来引入必要的 SDK: ```ruby pod 'Google-Ads-SDK' ``` 执行命令 `pod install` 安装所需的框架并打开 `.xcworkspace` 文件继续开发[^1]。 #### 初始化 SDK 及设置应用信息 按照官方文档指导,在应用程序启动时初始化 AdMob SDK 并传入之前获得的应用唯一标识符(App ID)。通常是在 AppDelegate.swift 中实现此操作: ```swift import UIKit import GoogleMobileAds @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { GADMobileAds.sharedInstance().start(completionHandler: nil) // 设置测试设备ID以便调试期间查看测试广告 let requestConfiguration = GADRequestConfiguration() requestConfiguration.testDeviceIdentifiers = ["YOUR_DEVICE_ID"] GADMobileAds.sharedInstance().requestConfiguration = requestConfiguration return true } } ``` #### 创建 SplashViewController 类处理开屏逻辑 新建一个 UIViewController 子类命名为 `SplashViewController` 负责加载和显示开屏广告。在此控制器内部定义属性存储广告实例以及负责渲染视图组件的对象。 ```swift import UIKit import GoogleMobileAds class SplashViewController: UIViewController, GADFullScreenContentDelegate { private lazy var interstitial: GADInterstitialAd? = { /* ... */ }() override func viewDidLoad() { super.viewDidLoad() loadAd() } /// 加载新的全屏广告 private func loadAd(){ let request = GADRequest() GADInterstitialAd.load(withAdUnitID:"ca-app-pub-3940256099942544/4411468910", request:request, completionHandler: {[weak self](ad,error) in guard error == nil else{return} self?.interstitial = ad self?.interstitial?.fullScreenContentDelegate=self DispatchQueue.main.asyncAfter(deadline:.now()+2){ if let strongSelf= self{ strongSelf.showAd() } } }) } /// 展示已经加载好的广告给用户看 private func showAd(){ interstitial?.present(fromRootViewController:self) } } ``` 上述代码片段展示了如何创建一个简单的开屏页面控制器 (`SplashViewController`) ,它会在两秒延迟后尝试呈现预先加载成功的插页式广告(`GADInterstitialAd`). 如果希望更灵活控制广告行为,则可以根据实际需求调整定时器间隔时间或改变触发条件. #### 处理广告生命周期事件 作为 `GADFullScreenContentDelegate` 协议的一部分,实现了几个回调方法用来监听广告的状态变化,例如当广告成功关闭之后可以导航至主界面或其他业务流程。 ```swift extension SplashViewController { /// 当广告被点击离开屏幕时调用 public func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) { print("Ad was dismissed.") // 导航到主界面或者其他地方... performSegue(withIdentifier: "showHome", sender: nil) } /// 当广告展示失败时调用 public func ad(_ ad: GADFullScreenPresentingAd,didFailToPresentWithError error: Error?) { print("Ad failed to present with error \(String(describing:error))") // 同样应该考虑在这种情况下采取行动,比如直接进入主页而不等待广告播放完毕。 performSegue(withIdentifier: "showHome", sender: nil) } } ``` 以上就是关于在 iOS 移动端接入 AdMob 开屏广告的大致过程介绍。需要注意的是,具体实现细节可能会随着 API 版本更新而有所变动;因此建议始终参照最新的官方文档进行学习实践。
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

carden_coder

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

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

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

打赏作者

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

抵扣说明:

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

余额充值