Flutter:无法在iOS上展示广告

问题描述

我将firebase_admob用于Flutter。我按照说明进行了设置,并且广告在Android上运行正常。 iOS唯一的额外步骤是将其添加Info.plist

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

我在iOS上没有任何错误,只是空横幅。

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,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();

在Firebase中创建了单独的Admob标语以及iOS应用,并在项目中包含了GoogleService-info.plist。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)