一:官方網(wǎng)址
AdMob 應用 ID: ca-app-pub-3940256099942544~1458002511
橫幅廣告測試專用廣告單元 ID:ca-app-pub-3940256099942544/2934735716
插頁式廣告測試專用廣告單元 ID:ca-app-pub-3940256099942544/4411468910
激勵廣告專用測試廣告單元 ID: ca-app-pub-3940256099942544/1712485313
原生高級廣告測試專用廣告單元 ID: ca-app-pub-3940256099942544/3986624511
1、使用CocoaPods
pod 'Google-Mobile-Ads-SDK'
pod install --repo-update
2、在info.plist里添加GADApplicationIdentifier鍵值對
(測試時,可以將AdMob 應用 ID: ca-app-pub-3940256099942544~1458002511放入),
賬號申請下來后,可以將真實的ID配置到info.plist里邊
3、創(chuàng)建單利manager
GADMobileAds.sharedInstance().start(completionHandler: nil),
然后運行時,會在consolg里打印出你的測試設備,
<Google> To get test ads on this device, set:
GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers =
@[ @"2077ef9a63d2b398840261c8221a0c9b" ];
將代碼添加到start后邊,然后就開始load廣告
4、開始load廣告單元(已插屏廣告為說明)
其余的demo可以看一下官方的示例
fileprivate func loadInterstitial() {
let request = GADRequest()
GADInterstitialAd.load(
withAdUnitID: "ca-app-pub-3940256099942544/4411468910", request: request
) { (ad, error) in
if let error = error {
print("Failed to load interstitial ad with error: \(error.localizedDescription)")
return
}
self.interstitial = ad
self.interstitial?.fullScreenContentDelegate = self
}
}
// MARK: - GADFullScreenContentDelegate
func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Ad will present full screen content.")
}
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error)
{
print("Ad failed to present full screen content with error \(error.localizedDescription).")
}
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Ad did dismiss full screen content.")
//重新加載下一個廣告
loadInterstitial()
}
重新加載下一個廣告loadInterstitial()很重要!?。】梢员WC你的下個廣告能夠提前加載出來
5、特別說明:測試時,必須使用測試廣告單元(利用#if DEBUG區(qū)分測試和正式)或者添加測試設備,不可用正式的廣告單元進行投放,會被封號
6、遇到的問題
A問題:Failed to load interstitial ad with error: Request Error: No ad to show.
A問題:Failed to load interstitial ad with error: Request Error: No ad to show.
這個問題,搜索了很多地方,最終查到原因是因為在Admob上需要設置app-ads.txt。

在這里看下是否app-ads.txt驗證通過,24小時生效。如果不通過,需要點擊“How to set up app-ads.txt”按鈕查看如何添加。
B問題:Error Domain=com.google.admob Code=5 "Request Error: The Google Ad request was unable to be fulfilled before a timeout occurred.
這個問題就很詭異,測試廣告單元ID是好的,但是用正式的廣告單元ID+設置測試設備ID后,就會出現(xiàn)這個錯誤,我找了很多資料,最后有一段話“多測試幾次,多拉幾次廣告就行了”,確實是這樣。
特別說明一下:app-ads.txt
官方解釋如下:
授權(quán)應用賣方(或 app-ads.txt) 是一項 IAB 計劃,可幫助保護你的應用廣告資源免遭廣告欺詐。你可以創(chuàng)建 app-ads.txt 文件來指明有權(quán)銷售你的廣告資源的賣方。通過指明授權(quán)賣方,你可以避免那些原本可能流向欺詐應用的仿冒廣告資源的廣告客戶支出。
app-ads.txt 文件是公開的,可供廣告交易平臺、供應方平臺 (SSP) 以及其他買方和第三方供應商抓取。
授權(quán)應用賣方 (app-ads.txt) 是授權(quán)數(shù)字賣方 (ads.txt) 計劃的延伸和擴展,后者最初設計用于保護網(wǎng)絡廣告資源。app-ads.txt 在 ads.txt 的基礎(chǔ)上擴展了兼容性, 使之支持移動應用中展示的廣告。
通俗來說,有了app-ads.txt像是有了保障,可以保證廣告來源的質(zhì)量。