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