iOS Xamarin Plugin

Getting started with the Adcash Xamarin for iOS plugin

Supported platforms:

  • iOS

The plugin features include:

  • Support for Banner Ads
  • Support for Interstitial Ads
  • Banner positions(top/bottom)
  • Banner ad events listeners
  • A sample project to demonstrate plugin integration

And the requirements are:

  • Xamarin/Visual Studio
  • An ad unit ID
  • Adcash® iOS SDK library
  • Target of 6.0 or higher
  • Recommended: CocoaPods

Adcash provides two different APIs:

  1. iOS binding API(AdcashSDK.iOS)
  2. C#-friendly API(AdcashSDK.Api) on top of the iOS API

Note: The iOS binding API would be a better choice if you need to make fine-tunings.

Run the project

If you are running the Test sample project you should be able to run the project now.

  1. You can choose to use the iOS binding API in an iOS-only project and explicitly specify a parent view for your banner:
     using AdcashSDK.iOS;
     using CoreGraphics;
     using Foundation;
     ...
    
     const string zoneID = "<Get your zone ID at www.adcash.com";
    
     public override void ViewDidLoad ()
     {
         base.ViewDidLoad ();
    
         ACBannerView banner = new ACBannerView (ACAdSize.ACAdSizeSmartBanner, zoneID, this);
         banner.WeakDelegate = this;
         this.View.AddSubview(banner); // Banner is of type ACBannerView, which is subclass of UIView
    
         // Place the banner at the bottom of the view
         banner.Frame = new CGRect (0, this.View.Frame.Size.Height - banner.Frame.Size.Height, this.View.Frame.Size.Width, banner.Frame.Size.Height);
    
         // Autoresize the view when rotating the device
         banner.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | 
                                   UIViewAutoresizing.FlexibleTopMargin | 
                                   UIViewAutoresizing.FlexibleLeftMargin | 
                                   UIViewAutoresizing.FlexibleRightMargin;
    
         banner.Load ();
     }
     ...
     //bind to the Objective-C selector bannerViewDidReceiveAd:
     [Export("bannerViewDidReceiveAd:")]
     public void BannerViewDidReceiveAd (ACBannerView bannerView)
     {
         Console.WriteLine("Adcash: Banner loaded successfully.");
     }
    
  2. Alternatively, you can display a banner in your view controller with the Adcash C#-friendly API:
     using AdcashSDK.API;
     ...
    
     const string zoneID = "<Get your zone ID at www.adcash.com";
    
     public override void ViewDidLoad ()
     {
         base.ViewDidLoad ();
    
         BannerView bannerView = new BannerView (zoneID, AdPosition.Bottom);
    
         bannerView.LoadAd ();
    
         bannerView.AdLoaded += (object sender, EventArgs e) => {
             Console.WriteLine("Loading banner");
             bannerView.Show();
         };
    
         bannerView.AdFailedToLoad += (object sender, AdFailedToLoadEventArgs e) => {
             Console.WriteLine("Failed to load banner");
             bannerView.Hide();
         };
     }
    

    When using the C#-friendly API you do not need to specify the parent view for the banner. Instead, the banner will be added to the top-most view of the view controller.

Interstitial

  1. You can use the iOS binding API API to show an interstitial:
     using AdcashSDK.iOS;
     using Foundation;
     ...
    
     const string zoneID = "<Get your zone ID at www.adcash.com";
     private ACInterstitial interstitial;
    
     public override void ViewDidLoad ()
     {
         base.ViewDidLoad ();
    
         this.interstitial = new ACInterstitial (zoneID);
         this.interstitial.WeakDelegate = this;
         this.interstitial.Load ();
     }
    
     [Export("interstitialDidReceiveAd:")]
     public void InterstitialDidReceiveAd (ACInterstitial interstitial)
     {
         // Interstitial must be told to present explicitly when loaded
         Console.WriteLine("Interstitial loaded successfully");
         interstitial.PresentFromRootViewController(this);
     }
    
  2. And here is how you can show an interstitial with the C#-friendly API:
     using AdcashSDK.Api;
     ...
    
     const string zoneID = "<Get your zone ID at www.adcash.com";
     private Interstitial interstitial;
    
     public override void ViewDidLoad ()
     {
         base.ViewDidLoad ();
    
         this.interstitial = new Interstitial (zoneID);
         this.interstitial.LoadAd ();
    
         this.interstitial.AdLoaded += (object sender, EventArgs e) => {
             // Interstitials must be told explicitly to show after they've been loaded
             interstitial.Show ();
         };
         this.interstitial.AdFailedToLoad += (object sender, AdFailedToLoadEventArgs e) => {
             Console.WriteLine("failed to load interstitial");
         };
     }
    

Ad Events

Note: The following example is only applicable with the C#-friendly API.

Both BannerView and Interstitial classes contain the same ad events that you can register for. These events are of type EventHandler. Here is an example of how to register ad events on a banner:

using AdcashSDK;
...

BannerView bannerView = new BannerView("YOUR_AD_UNIT_ID", AdPosition.Top);
bannerView.LoadAd();

// Called when an ad request has successfully loaded.
bannerView.AdLoaded += HandleAdLoaded;
// Called when an ad request failed to load.
bannerView.AdFailedToLoad += HandleAdFailedToLoad;
// Called when an ad is clicked.
bannerView.AdOpened += HandleAdOpened;
// Called when the user is about to return to the app after an ad click.
bannerView.AdClosing += HandleAdClosing;
// Called when the user returned from the app after an ad click.
bannerView.AdClosed += HandleAdClosed;
// Called when the ad click caused the user to leave the application.
bannerView.AdLeftApplication += HandleAdLeftApplication;
...

public void HandleAdLoaded(object sender, EventArgs args)
{
    print("HandleAdLoaded event received.");
    // Handle the ad loaded event.
}

The only event with special event arguments is AdFailedToLoad. It passes an instance of AdFailedToLoadEventArgs with a Message describing the error:

public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
  print("Interstitial Failed to load: " + args.Message);
  // Handle the ad failed to load event.
};

You only have to register for the events you need to use.

Conversion tracking

If you are an advertiser, you might want to send us some information when your app has been opened so that we can track a successful conversion. Keep in mind that only the first time the app is opened is considered a successful conversion.

You can do this using the ConversionTracker class:

using AdcashSDK.Api;

public class AppDelegate : UIApplicationDelegate
...
public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
    var tracker = new ConversionTracker ();
    tracker.ReportAppOpen ();

    return true;
}

Sample project

You will find a sample project in the component archive where you can also see in details how to integrate the Adcash SDK into your application.

Support

If you need any support or assistance you can contact the mobile team by sending an email to [email protected].

  • Did you find this article helpful?
  •    
Tagged: