Forum Xamarin.Forms

How to use Facebook Audience in Xamarin Forms

adam_madam_m Member ✭✭
edited March 20 in Xamarin.Forms

I am trying to implement Facebook audience in xamarin forms. I have installed xamarin.facebook.audiencenetwork.android NuGet package for android and xamarin.facebook.audiencenetwork.ios for ios

I have implemented android but I am stuck in ios. I have referred to implement and the code builds fine but when I run it the UI fails to load and shows me on UIApplication.Main(args, null, "AppDelegate");

Specified cast is invalid

This is my code for controlview

`using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Xamarin.Forms;

namespace Stock.Services
{
public class AdControlView : ContentView
{

    public static BindableProperty PlacementIdProperty = BindableProperty.Create(nameof(PlacementId), typeof(string),
        typeof(AdControlView), null);
    public string PlacementId
    {
        get { return (string)GetValue(PlacementIdProperty); }
        set { SetValue(PlacementIdProperty, value); }
    }

    public static BindableProperty SizeProperty = BindableProperty.Create(nameof(Size), typeof(FacebookAdSize),
        typeof(AdControlView), FacebookAdSize.BannerHeight50);


    public FacebookAdSize Size
    {
        get { return (FacebookAdSize)GetValue(SizeProperty); }
        set { SetValue(SizeProperty, value); }
    }

    public event EventHandler Loaded;
    public event EventHandler Click;
    public event EventHandler<ErrorEventArgs> Error;
    public event EventHandler Impression;

    public virtual void OnLoaded()
    {
        Loaded?.Invoke(this, EventArgs.Empty);
        Debug.WriteLine("FACEBOOK ADS LOADED");
    }

    public virtual void OnClick()
    {
        Click?.Invoke(this, EventArgs.Empty);
        Debug.WriteLine("FACEBOOK ADS CLICKED");
    }

    public virtual void OnImpression()
    {
        Impression?.Invoke(this, EventArgs.Empty);
        Debug.WriteLine("FACEBOOK ADS Impression");
    }

    public void OnError(int errorCode, string errorMessage)
    {
        Error?.Invoke(this, new ErrorEventArgs(errorCode, errorMessage));
        Debug.WriteLine("FACEBOOK ADS ERROR: " + errorMessage);
        //Settings.FbAdsError = errorMessage;
        //Debug.WriteLine("Settings.FbAdsError: " + Settings.FbAdsError);
    }

    public enum FacebookAdSize
    {
        Banner32050,
        BannerHeight50,
        BannerHeight90,
        Interstitial,
        RectangleHeight250
    }

    public class ErrorEventArgs : EventArgs
    {
        public ErrorEventArgs(int errorCode, string errorMessage)
        {
            ErrorCode = errorCode;
            ErrorMessage = errorMessage;
        }

        public int ErrorCode { get; }
        public string ErrorMessage { get; }

    }
}

}`

and this is code where I use my controller

`private void AddFacebookAdsControl()
{
try
{
AdControlView fbAdsControl = new AdControlView();

            fbAdsControl.HeightRequest = 60;
            //We create this on developer facebook website
            switch (Device.RuntimePlatform)
            {
                case Device.iOS:
                    fbAdsControl.PlacementId = "PlacementIDforIOS";
                    break;
                case Device.Android:
                    fbAdsControl.PlacementId = "PlacementIDforAndroid";
                    break;
            }


            fbAdsControl.Size = AdControlView.FacebookAdSize.Banner32050;

            fbAdsControl.VerticalOptions = LayoutOptions.EndAndExpand;

            StackLayout.Children.Add(fbAdsControl);
        }
        catch(Exception ex)
        {

        }
    }`

And this is my custom renderer for ios

`
[assembly: ExportRenderer(typeof(AdControlView), typeof(AdControlViewRenderer))]
public class AdControlViewRenderer : UIViewController, IAdViewDelegate
{
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.

    var adView = new AdView("PlacementID", AdSizes.BannerHeight50, this) { Delegate = this };
    adView.Frame = new CGRect(0, 20, adView.Bounds.Width, adView.Bounds.Height);
    adView.LoadAd();
    View.AddSubview(adView);
}

[Export("adViewDidLoad:")]
public void AdViewDidLoad(AdView adView)
{
    Console.WriteLine("Ad was loaded and ready to be displayed");
}

[Export("adView:didFailWithError:")]
public void AdViewDidFail(AdView adView, NSError error)
{
    Console.WriteLine("Ad failed to load");
}

[Export("adViewDidClick:")]
public void AdViewDidClick(AdView adView)
{
    Console.WriteLine("Banner ad was clicked.");
}

[Export("adViewDidFinishHandlingClick:")]
public void AdViewDidFinishHandlingClick(AdView adView)
{
    Console.WriteLine("Banner ad did finish click handling.");
}

[Export("adViewWillLogImpression:")]
public void AdViewWillLogImpression(AdView adView)
{
    Console.WriteLine("Banner ad impression is being captured.");
}

}`

Idk what I am doing wrong, any kind of help is really helpful or links where I can read the implementation. Thanks in advance.

Best Answer

Answers

Sign In or Register to comment.