Ad load errors

AdMob is a company that provided the ability to display ads in your mobile app. It is now a part of Google, and hence you may hear it referred as Firebase Ads or Google thiết bị di động Ads. You can use this on quả táo or Android. No other platform is currently supported at the time of this post.

Bạn đang xem: Ad load errors

I have working sample code in my AdMob Github Repo.

Setup Account

Once setup, press Get Started, and create a new App. You will receive an AdMob AppId, similar to lớn this: ca-app-pub-XXXXXXXXXXXXXXXX~XXXXXXXXXXYou will need lớn create one for Android & one for iOS

Keep these handy, you will need them soon.

Create Ad Unit

Next you need to lớn create an Ad Unit. The most common, would be a Banner.

*

If you create your own, they vày take some time to lớn come through, và you will notice when trying to lớn view your ad unit, you will get this error message.

There was a problem getting an ad response. ErrorCode: 0 Failed lớn load ad:0Hence you can use kiểm tra ones initially, while getting your layout right.

You will get an Id lượt thích this, that is different for both iOS & Android.

Xem thêm: Mặt Nạ Sữa Non Bà Già Nga Màu Vàng (Trẻ Hóa Dưỡng Trắng), Mặt Nạ Sữa Non Bà Già Nga Màu Vàng

ca-app-pub-3940256099942544/2934735716

Xamarin.Forms View

The implementation of AdView is different on each platform, but we need a common control to lớn use in Xamarin.Forms. Create a new class as below. We have an AdUnitId as we will need to lớn pass that through to the AdView control.

public class AdMobView : View public static readonly BindableProperty AdUnitIdProperty = BindableProperty.Create( nameof(AdUnitId), typeof(string), typeof(AdMobView), string.Empty); public string AdUnitId get => (string)GetValue(AdUnitIdProperty);set => SetValue(AdUnitIdProperty, value);

Android

You may have khổng lồ use a version lower than the latest, due to conflicts with the latest version of Xamarin.Forms.Before Xamarin.Forms.Init() in your MainActivity, add the following codeAndroid.Gms.Ads.MobileAds.Initialize(ApplicationContext, "YOUR game android APP ID HERE");Make sure you địa chỉ cửa hàng the following permissions lớn your android project.Go to lớn the properties of your game android project, then app android Manifest, then select the required permissions.

ACCESS_NETWORK_STATEINTERNETNow go khổng lồ your AndroidManifest.xml (expand the Properties item) and địa chỉ the following activity, in between the tags.Finally we need to add the CustomRenderer. Create a new class in your apk project, & create the following renderer.namespace Admob.Droidpublic class AdMobViewRenderer : ViewRendererpublic AdMobViewRenderer(Context context) : base(context) protected override void OnElementChanged(ElementChangedEventArgs e)base.OnElementChanged(e);if (e.NewElement != null && Control == null)SetNativeControl(CreateAdView());protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)base.OnElementPropertyChanged(sender, e);if (e.PropertyName == nameof(AdView.AdUnitId))Control.AdUnitId = Element.AdUnitId;private AdView CreateAdView()var adView = new AdView(Context)AdSize = AdSize.SmartBanner,AdUnitId = Element.AdUnitId;adView.LayoutParameters = new LinearLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent); adView.LoadAd(new AdRequest.Builder().Build());return adView;

iOS

Before Xamarin.Forms.Init() in your AppDelegate, địa chỉ the following code.Google.MobileAds.MobileAds.Configure("YOUR IOS tiện ích ID HERE");Now add the following renderer in your quả táo project.namespace Admob.iOSpublic class AdMobViewRenderer : ViewRendererprotected override void OnElementChanged(ElementChangedEventArgs e)base.OnElementChanged(e);if (Control == null)SetNativeControl(CreateBannerView());protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)base.OnElementPropertyChanged(sender, e);if (e.PropertyName == nameof(BannerView.AdUnitID))Control.AdUnitID = Element.AdUnitId;private BannerView CreateBannerView()var bannerView = new BannerView(AdSizeCons.SmartBannerPortrait)AdUnitID = Element.AdUnitId,RootViewController = GetVisibleViewController();bannerView.LoadRequest(GetRequest());Request GetRequest()var request = Request.GetDefaultRequest();return request;return bannerView;private UIViewController GetVisibleViewController()var windows = UIApplication.SharedApplication.Windows;foreach (var window in windows)if (window.RootViewController != null)return window.RootViewController;return null;

Xamarin.Forms

Now we want to view it on Xamarin.Forms. You can địa chỉ cửa hàng the banner lớn your app, by adding the control khổng lồ your page, as per below.

You will want khổng lồ change the AdUnitId, depending upon which platform you are on. You can use a simple if statement & retrieve the appropriate key.

if (Device.RuntimePlatform == Device.iOS) AdUnitId = "iOS Key";else if (Device.RuntimePlatform == Device.Android) AdUnitId = "Android Key";

*
*