James Montemagno
James Montemagno

Live, Love, Bike, and Code.

Live, Love, Bike, and Code

Share


Tags


James Montemagno

Cross-Platform In-App Purchases for Xamarin.Mac Apps!

This week, one of my best friends Frank Krueger released his brilliant app iCircuit 3D for macOS (he also wrote an awesome blog on how it was made). While I know that catalyst support for Xamarin (and .NET MAUI) is coming soon, I still think there are great opportunities to monetize macOS apps written in C# today (see iCircuit 3D!). I released my In-App Billing Plugin a few years ago for iOS, Android, and UWP apps and it has been awesome to hear how it has helped so many developers monetize their apps. The library has expanded over the years to support subscriptions and updated to the latest Android billing libraries. I have used this library personally to monetize my apps including Island Tracker and My Cadence. If you haven't seen the API it is absolutely lovely! Check this out:

Get Product Info

Everything is basically three lines of code... Connect, Call, Disconnect:

var connected = await CrossInAppBilling.Current.ConnectAsync();
if(!connected)
    return;
    
var items = await CrossInAppBilling.Current.GetProductInfoAsync(ItemType.InAppPurchase, "myitem");

await CrossInAppBilling.Current.DisconnectAsync();

Purchase an Item

Rinse and repeat!

var connected = await CrossInAppBilling.Current.ConnectAsync();
if(!connected)
    return;
    
var purchase = await CrossInAppBilling.Current.PurchaseAsync(productId, ItemType.InAppPurchase);
if(purchase != null & purcxhase.State == PurchaseState.Purchased)
{
	//item bought
}

await CrossInAppBilling.Current.DisconnectAsync();

Now for Xamarin.Mac!

I recently had a few user of My Stream Timer as me how they can donate and support the development of the app. I never really thought about monetizing the app as I made it sort of for myself to show a countdown before my streams. However, this app has grown to nearly 20,000 downloads from all sorts of streamers including yoga instructors, teachers, coders, and gamers! So I figure I would extend support for my plugin to macOS. This weekend I sat down and 3 hours later it was done! Luckily for me the API is extremely similar to iOS with StoreKit. There are a few differences around making the actual purchase where you can't specify a string id, but instead must get the actual product from StoreKit first. Luckily this method already existed in my code and after a bit of hacking I got it working. Oh and there is no TransactionReceipt, which is weird but there are newer APIs that developers are supposed to query. Beyond that the API works as expected! Check it out!

In app purchase in My Stream Timer

My favorite part has to be testing IAPs on macOS because it is so simple. All you need to do is log out of the app store and then open your app in debug and make the purchase! It just works! I love it!

If you are making macOS apps I hope that you will checkout the plugin and try to add some monetization to your app!

Copyright © James Montemagno 2021 All rights reserved. Privacy Policy

View Comments