Flutter: can't show ads on iOS

85 views Asked by At

I use firebase_admob for Flutter. I followed the instructions for the setup and the ads are working fine for Android. The only extra step for iOS is adding this to the Info.plist:

<key>GADApplicationIdentifier</key>
<string>[ADMOB_APP_ID]</string>

I don't get any error on iOS, just empty banners.

import 'dart:io';

import 'package:firebase_admob/firebase_admob.dart';
import 'package:my_rents/services/auth.dart';

final String appId = Platform.isAndroid
    ? 'ca-app-pub-code1'
    : 'ca-app-pub-code2';
final String bannerId = Platform.isAndroid
    ? 'ca-app-pub-code3'
    : 'ca-app-pub-code4';
final String interstitialId = Platform.isAndroid
    ? 'ca-app-pub-code5'
    : 'ca-app-pub-code6';

MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo(
  keywords: <String>['rents', 'state agency', 'investments'],
  childDirected: false,
  testDevices: <String>[], // Android emulators are considered test devices
);

class Ads {
  bool showAds = false;
  static bool disposed;
  bool initialized;
  static BannerAd myBanner;
  static InterstitialAd myInterstitial = InterstitialAd(
    adUnitId: interstitialId,
    targetingInfo: targetingInfo,
  );
  Ads();

  static init() {
    FirebaseAdMob.instance.initialize(appId: appId);
  }

  BannerAd _createBanner() {
    return BannerAd(
      adUnitId: bannerId,
      size: AdSize.smartBanner,
      targetingInfo: targetingInfo,
      listener: (MobileAdEvent event) {
        if (event == MobileAdEvent.loaded) if (disposed)
          myBanner.dispose();
        else
          showBanner();
      },
    );
  }

  loadBanners() {
    disposed = false;
    if (myBanner == null) {
      myBanner = _createBanner();
    }
    myBanner.load();
    myInterstitial..load();
    initialized = true;
  }

  static showBanner() {
    if (myBanner != null) {
      myBanner..show(anchorOffset: 0.0, anchorType: AnchorType.top);
    }
  }

  static void cancelBanner() {
    if (myBanner != null) {
      Future.delayed(const Duration(milliseconds: 500), () {
        disposed = true;
        myBanner?.dispose();
        myBanner = null;
      });
    }
  }

  static showInterstitial() async {
    // TODO: remove this line after showing ads in iOS
    if (Platform.isAndroid) {
      if (authService.accountType.length == 0) {
        myInterstitial
          ..show(
            anchorType: AnchorType.bottom,
            anchorOffset: 0.0,
            horizontalCenterOffset: 0.0,
          );
      }
    }
  }
}

final Ads ads = Ads();

Separate Admob banners created as well as iOS app in Firebase and included GoogleService-info.plist to the project.

0

There are 0 answers