現在免費小游戲及應用的主要收入渠道就是通過接入廣告。而Google的Admob適用於全球范圍內的廣告接入,文檔方面及后台管理也是較為完善,接入還是比較便捷的。
不過Google目前還在牆外,雖然接入后廣告不需要vpn就可以顯示訪問,但是官網設置及文檔還是需要梯子的。
1、在admob網站注冊帳號等。 https://apps.admob.com/
2、在登錄后點擊 通過新的應用獲利 按鈕即可創建新的平台廣告位。
3、添加完對應廣告位后即可在 管理您的應用 按鈕中找到添加的項目, 點擊后可以查看應用廣告具體的信息。
申請后可以得到一個 adUnitID,這在后面代碼中需要用到。即下圖的廣告單元ID。

官方文檔: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商店也可以正常運行顯示了。
官方文檔: 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:0]; } [[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項內容注釋。
