How to Expose HUAWEI Ads Lite features in Xamarin.Forms

Furkan Tüzün
3 min readApr 28, 2021

Hello everyone,
In this article, I will explain how to expose HUAWEI Ads Lite features in Xamarin.Forms.

Installing the Huawei Ads Lite NuGet Package

HUAWEI Ads Lite is available as a NuGet package. To use it, open NuGet Package Manager and install Huawei.Hms.AdsLite. For details, please refer to Installing the Huawei Ads Lite NuGet Package.

Manifest & Permissions

To allow HTTP network requests on devices with targetSdkVersion 28 or later, configure the following information in the AndroidManifest.xml file.

To use the Ads kit without error, we must apply for the permissions below.

Creating Custom Renderer for Banner Ad

To use Banner Ad in the application, we have to expose it as a View. So we will create a custom renderer for the BannerView control. For details about Custom Renderers please refer to Custom Renderers Introduction.

A custom BannerView control must be derived from the Xamarin.Forms.View class, as shown in the following code example:

The CustomViewControl control is rendered by platform-specific renderer class, which derive from the ViewRenderer class.

Using Reward Ad with DependencyService

We will use the DependencyService to use the Reward Ad features in the shared project. For details about DependencyService please refer to DependencyService Introduction.

Create an interface in the shared project.

After creating the interface, the interface must be implemented in Xamarin.Android project.

Banner and Reward Ads are now ready for use in the shared project.

I developed a small game in the app to make the Reward Ad a little more meaningful. In the game, the user tries to catch the alien within 60 seconds and the score increases each time user catches it. When the time is up, the user is given the right to choose, if the advertisement is watched, 60 seconds will be added to the game, if the advertisement is not watched, the game is over :)

To use Banner Ad we define CustomViewControl in MainPage.xaml.

To use the Reward Ad, we define a new object from the DependencyService class in the MainPage.cs page. We call events and methods that we add to the interface.

In this way, we have successfully exposed the Banner and Reward Ad formats.

Banner Ad Format

Reward Ad Format

--

--