Skip to content

Latest commit

 

History

History
151 lines (115 loc) · 5.32 KB

GettingStarted.md

File metadata and controls

151 lines (115 loc) · 5.32 KB

Getting Started

Setup

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

Using Connectivity 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();
        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();
        if(!connected)
            return false;
        
        //make additional billing calls
    
    }
    finally
    {
        await billing.DisconnectAsync();
    }
}

Disposing of In-App Billing Plugin

This plugin also implements IDisposable on all implementations. This ensure that all events are unregistered from the platform. This include unregistering from the SKPaymentQueue on iOS. Only dispose when you need to and are no longer listening to events and you must call Dispose on the actual static class. The next time you gain access to the CrossInAppBilling.Current a new instance will be created.

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

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

It is recommended to not us a using statement, but instead just call the single Dispose on the static class, which will also dispose the Current:

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

    var billing = CrossInAppBilling.Current;
    
    try
    {
        var connected = await billing.ConnectAsync();
        if(!connected)
            return false;
        
        //make additional billing calls
    
    }
    finally
    {
        await billing.DisconnectAsync();
    }
    //This does all the disposing you need
    CrossInAppBilling.Dispose();
}

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 Considerations

Android:

The com.android.vending.BILLING permission is required to use In-App Billing on Android and this library ill 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);
}

<= Back to Table of Contents