前言
現(xiàn)在免費小游戲及應(yīng)用的主要收入渠道就是通過接入廣告。而Google的Admob適用于全球范圍內(nèi)的廣告接入,文檔方面及后臺管理也是較為完善,接入還是比較便捷的。
不過Google目前還在墻外,雖然接入后廣告不需要vpn就可以顯示訪問,但是官網(wǎng)設(shè)置及文檔還是需要梯子的。
Admob應(yīng)用廣告申請設(shè)置
1、在admob網(wǎng)站注冊帳號等。https://apps.admob.com/
2、在登錄后點擊“通過新的應(yīng)用獲利”按鈕即可創(chuàng)建新的平臺廣告位。
3、添加完對應(yīng)廣告位后即可在“管理您的應(yīng)用”按鈕中找到添加的項目, 點擊后可以查看應(yīng)用廣告具體的信息。
申請后可以得到一個adUnitID,這在后面代碼中需要用到。即下圖的廣告單元ID。
Android接入
官方文檔:https://developers.google.com/admob/android/existing-app
項目環(huán)境配置:
1、Android Jdk必須升級到1.7.0以上,Android sdk要升級到Android5.0以上。
2、從SDK Manager中下載安裝Google Play services并且在我們應(yīng)用項目添加引用。
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" />
實現(xiàn)代碼官網(wǎng)上都有:https://developers.google.com/admob/android/interstitial
這里不在重復(fù)說明,說一個注意事項:測試手機需要裝有Google Play 商店,否則會提示 Google Play services is missing,并且應(yīng)用直接崩潰。
補充: 后來發(fā)現(xiàn)接入了Google 排行榜后,沒有裝Google Play商店也可以正常運行顯示了。
IOS接入
官方文檔:https://developers.google.com/mobile-ads-sdk/docs/admob/ios/quick-start?hl=zh-cn
實現(xiàn)步驟及代碼同樣都在官網(wǎng)上,這里只講下如果不是直接在游戲的 UIViewController中調(diào)用廣告顯示的情況處理。
此時顯示需要如下:
if ([self.interstitial isReady]) {
CCLOG("ready");
UIApplication* clientApp = [UIApplication sharedApplication];
UIWindow* topWindow = [clientApp keyWindow];
if (!topWindow)
{
topWindow = [[clientApp windows] objectAtIndex:0];
}
[[topWindow rootViewController] presentViewController:self animated:NO completion:nil];
[self.interstitial presentFromRootViewController:self];
}
else{
CCLOG("not ready");
}
同時關(guān)閉廣告如下處理:
- (void)interstitialDidDismissScreen:(GADInterstitial *)interstitial {
[self dismissViewControllerAnimated:NO completion:nil];
self.interstitial = [self createAndLoadInterstitial];
}
其他碰到的幾個問題:
1、找不到添加的SDK庫導(dǎo)致編譯不過。
解決:官網(wǎng)下載的SDK包必須在Mac上進行解壓。 不然Framework內(nèi)的引用會不見。
2、[self.interstitial isReady] 返回值一直是false, interstitialDidReceiveAd等回調(diào)也收不到消息。
解決: 在info.plist一定要添加以下項。
<key>UIBackgroundModes</key>
<array>
<string>remote-notification</string>
</array>
3、顯示的廣告一直是 you're displaying an interstitial test ad from admob.
解決: 將測試 的 testDevices項內(nèi)容注釋。