如何解决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 举报,一经查实,本站将立刻删除。