Admob - Google广告接入

2023-07-12,,

前言

  现在免费小游戏及应用的主要收入渠道就是通过接入广告。而Google的Admob适用于全球范围内的广告接入,文档方面及后台管理也是较为完善,接入还是比较便捷的。

不过Google目前还在墙外,虽然接入后广告不需要vpn就可以显示访问,但是官网设置及文档还是需要梯子的。

Admob应用广告申请设置

   1、在admob网站注册帐号等。  https://apps.admob.com/

  2、在登录后点击   通过新的应用获利  按钮即可创建新的平台广告位。

  3、添加完对应广告位后即可在   管理您的应用   按钮中找到添加的项目, 点击后可以查看应用广告具体的信息。

申请后可以得到一个 adUnitID,这在后面代码中需要用到。即下图的广告单元ID。

    

Android接入

  官方文档:https://developers.google.com/admob/android/existing-app

项目环境配置:

  1、Android Jdk必须升级到1.7.0以上,Android sdk要升级到Android5.0以上。

  2、从SDK Manager中下载安装Google Play services并且在我们应用项目添加引用。

  3、AndroidManifest.xml文件中添加清单如下,

    <!-- Include required permissions for Google Mobile Ads to run-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <!--This meta-data tag is required to use Google Play Services.-->
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" /> <!--Include the AdActivity configChanges and theme. -->
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />

实现代码官网上都有: https://developers.google.com/admob/android/interstitial

这里不在重复说明,说一个注意事项:测试手机需要装有Google Play 商店,否则会提示  Google Play services is missing,并且应用直接崩溃。

补充: 后来发现接入了Google 排行榜后,没有装Google Play商店也可以正常运行显示了。

IOS接入

       官方文档: https://developers.google.com/mobile-ads-sdk/docs/admob/ios/quick-start?hl=zh-cn

实现步骤及代码同样都在官网上,这里只讲下如果不是直接在游戏的 UIViewController中调用广告显示的情况处理。

此时显示需要如下:

    if ([self.interstitial isReady]) {
CCLOG("ready");
UIApplication* clientApp = [UIApplication sharedApplication];
UIWindow* topWindow = [clientApp keyWindow];
if (!topWindow)
{
topWindow = [[clientApp windows] objectAtIndex:];
}
[[topWindow rootViewController] presentViewController:self animated:NO completion:nil]; [self.interstitial presentFromRootViewController:self];
}
else{
CCLOG("not ready");
}

同时关闭广告如下处理:

- (void)interstitialDidDismissScreen:(GADInterstitial *)interstitial {
[self dismissViewControllerAnimated:NO completion:nil];
self.interstitial = [self createAndLoadInterstitial];
}

其他碰到的几个问题:

  1、找不到添加的SDK库导致编译不过。

解决:官网下载的SDK包必须在Mac上进行解压。 不然Framework内的引用会不见。

  2、[self.interstitial isReady] 返回值一直是false, interstitialDidReceiveAd等回调也收不到消息。

解决: 在info.plist一定要添加以下项.

    <key>UIBackgroundModes</key>
<array>
<string>remote-notification</string>
</array>

  3、显示的广告一直是 you're displaying an interstitial test ad from admob.

解决: 将测试 的 testDevices项内容注释。

Admob - Google广告接入的相关教程结束。

《Admob - Google广告接入.doc》

下载本文的Word格式文档,以方便收藏与打印。