Skip to content

Latest commit

 

History

History
117 lines (85 loc) · 5.08 KB

GettingStarted.md

File metadata and controls

117 lines (85 loc) · 5.08 KB

Getting Started

Setup

  • NuGet: Plugin.InAppBilling NuGet
  • PM> Install-Package Plugin.InAppBilling
  • Install into ALL of your projects, include client projects.

Using InAppBilling APIs

It is drop dead simple to gain access to the In-App Billing APIs in any project. All you need to do is get a reference to the current instance of IInAppBilling via CrossInAppBilling.Current. Before making any calls to InAppBilling you must use ConnectAsync to ensure a valid connection to the app store of the device and always ensure that you call DisconnectAsync when you are finished. It is recommended to call DisconnectAsync inside of a finally block.

public async Task<bool> MakePurchase()
{
    var billing = CrossInAppBilling.Current;
    try
    {
        var connected = await billing.ConnectAsync(ItemType.InAppPurchase);
        if(!connected)
            return false;
        
        //make additional billing calls
    }
    finally
    {
        await billing.DisconnectAsync();
    }
}

There may be instances where you install a plugin into a platform that it isn't supported yet. This means you will have access to the interface, but no implementation exists. You can make a simple check before calling any API to see if it is supported on the platform where the code is running. This if nifty when unit testing:

public async Task<bool> MakePurchase()
{
    if(!CrossInAppBilling.IsSupported)
        return false;

    try
    {
        var billing = CrossInAppBilling.Current;
        var connected = await billing.ConnectAsync(ItemType.InAppPurchase);
        if(!connected)
            return false;
        
        //make additional billing calls
    
    }
    finally
    {
        await billing.DisconnectAsync();
    }
}

In-App Billing Recommended Reading

Due to the complex nature of In-App Billing I highly recommend reading exactly how they work on each platform before you start using this plugin:

In addition to this core reading I recommend the following:

Creating an In-App Purchase

Each app store has you create them in a different area.

  • Apple: Go to iTunes Connect -> Select App -> Features -> In-App Purchases
  • Android: Go to Google Play Console -> Select App -> Store presence -> In-app products (you can only create on if you have uploaded a version of your app with this plugin or the Vending permission set).
  • Microsoft: Go to Dashboard -> Select App -> Add-ons

Permissions & Additional Setup - Version 4

In version 4 we use Xamarin.Essentials so you must ensure you initialize it in your Android project. It is setup by default in new projects:

protected override void OnCreate(Bundle savedInstanceState) {
    //...
    base.OnCreate(savedInstanceState);
    Xamarin.Essentials.Platform.Init(this, savedInstanceState); // add this line to your code, it may also be called: bundle
    //...

That's it.

Permissions & Additional Setup - Version 2

Android:

The com.android.vending.BILLING permission is required to use In-App Billing on Android and this library will automatically added it your Android Manifest when you compile. No need to add them manually!

You must place this code in your Main/Base Activity where you will be requesting purchases from.

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);
    InAppBillingImplementation.HandleActivityResult(requestCode, resultCode, data);
}

Android Current Activity Setup

This plugin uses the Current Activity Plugin to get access to the current Android Activity. Be sure to complete the full setup if a MainApplication.cs file was not automatically added to your application. Please fully read through the Current Activity Plugin Documentation. At an absolute minimum you must set the following in your Activity's OnCreate method:

Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity = this;

It is highly recommended that you use a custom Application that are outlined in the Current Activity Plugin Documentation](https://github.com/jamesmontemagno/CurrentActivityPlugin/blob/master/README.md)

<= Back to Table of Contents