I am trying to do the (seemingly) simple task of integrating Native AdMob ads into my iOS app running on Swift. Let me first show you my storyboards and code for integration and then we'll move on to things I've tried to fix it.
Set-Up: Storyboards and Code
In my App Delegate, I configure Firebase a Google Mobile Ads...
import Firebase
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
GADMobileAds.configure(withApplicationID: "ca-app-pub-8482280186158418~7106629637")
GADMobileAds.sharedInstance().start(completionHandler: nil)
}
}
I have also added my GADApplicationIdentifier to my Info.plist. In my storyboard, I have a ViewController that contains a UICollectionViewCell that contains a GADUnifiedNativeAdView and the native elements are linked through outlets.
Over in my ViewController, in the viewDidLoad, I load 5 native ads.
override func viewDidLoad() {
super.viewDidLoad()
// Load Ads
let adUnitID = "ca-app-pub-{adUnitId}"
let numAdsToLoad = 5
let options = GADMultipleAdsAdLoaderOptions()
options.numberOfAds = numAdsToLoad
let imageOptions = GADNativeAdImageAdLoaderOptions()
imageOptions.disableImageLoading = false
let mediaOptions = GADNativeAdMediaAdLoaderOptions()
mediaOptions.mediaAspectRatio = .square
let adOptions = GADNativeAdViewAdOptions()
adOptions.preferredAdChoicesPosition = .topLeftCorner
adLoader = GADAdLoader(adUnitID: adUnitID, rootViewController: self, adTypes: [.unifiedNative], options: [options, imageOptions, mediaOptions])
adLoader.delegate = self
adLoader.load(GADRequest())
}
Over in the GADUnifiedNativeAdLoaderDelegate, I receive the ad and ad them to an array.
var nativeAds = [GADUnifiedNativeAd]()
extension ViewController: GADUnifiedNativeAdLoaderDelegate {
func adLoader(_ adLoader: GADAdLoader,
didFailToReceiveAdWithError error: GADRequestError) {
print("NATIVE AD - didFailToReceiveAdWithError: \(error)")
}
func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADUnifiedNativeAd) {
print("NATIVE AD - didReceive: \(nativeAd)")
nativeAds.append(nativeAd)
}
func adLoaderDidFinishLoading(_ adLoader: GADAdLoader) {
print("NATIVE AD - adLoaderDidFinishLoading")
libraryCollection.reloadData()
}
}
In my UICollectionView's delegate, in cellForItemAt I set the cell's class
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if isAdCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "adCell", for: indexPath) as! AdCollectionViewCell
if nativeAds.count == 5 {
cell.nativeAd = nativeAds[0]
cell.nativeAd.rootViewController = self
}
cell.setAdData()
return cell
}
Finally, in my UICollectionViewCell's class, I set the ad data
import Firebase
class AdCollectionViewCell: UICollectionViewCell {
@IBOutlet var adView: GADUnifiedNativeAdView
var nativeAd = GADUnifiedNativeAd()
func setAdData() {
adView.nativeAd = nativeAd
(adView.headlineView as! UILabel).text = nativeAd.headline
adView.callToActionView?.isUserInteractionEnabled = false
(adView.callToActionView as! UILabel).text = nativeAd.callToAction
adView.mediaView?.mediaContent = nativeAd.mediaContent
adView.mediaView?.contentMode = .scaleAspectFill
}
}
The Error
Now, whenever I use the test native ad unit id provided by Google AdMob, ca-app-pub-3940256099942544/3986624511, everything works just fine! I get no errors and the system prints
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283bbdc00>
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283bbd7a0>
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283bc73a0>
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283ba9880>
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283ba9810>
NATIVE AD - adLoaderDidFinishLoading
But the problem comes when I try to use my own native ad unit id. Upon loading, the system prints the error
NATIVE AD - didFailToReceiveAdWithError: Error Domain=com.google.admob Code=1 "Request Error: No ad to show." UserInfo={NSLocalizedDescription=Request Error: No ad to show., gad_response_info= ** Response Info **
Response ID: ymbzXou2CcrohQb16bzgBA
Network: (null)
** Mediation line items **
Entry (1)
Network: GADMAdapterGoogleAdMobAds
Credentials:
{
}
Error: Error Domain=com.google.admob Code=1 "Request Error: No ad to show." UserInfo={NSLocalizedDescription=Request Error: No ad to show.}
Latency: 0.095
}
for each ad that I load. And I have tried MANY things to fix this that I have found all over the internet...
Troubleshooting
As I mentioned, I have scoured the internet for help and troubleshooting options. Here is what I have done.
- Created a new native ad unit id
- Waited over a week to confirm the error with the unit id
- Checked my AdMob and AdSense account information
- Checked my AdMob and AdSense payment information
- Made sure the GADApplicationIdentifier in the Info.plist is correct
- Added all of the requirements to the
App Transport Security Settingsin the Info.plist as laid out by AdMob. (Allow Arbitrary Loads, Allows Arbitrary Loads for Media, Allow Arbitrary Loads in Web Content) - Double, triple, quadruple, quintuple checked the code and followed the documentation provided by AdMob
- Followed the CodeLab provided by AdMob
