Xamarin.iOS makes it easy to use Apple’s StoreKit API to include In App Purchasing in our iOS mobile applications. However, there’s quite a bit of repetitive code that has to be created for every app that includes it.
That’s where the new Xamarin.InAppPurchase component can help!
Using Xamarin.InAppPurchase, we can easily and quickly add iTunes App Store In App Purchasing of products, features or subscriptions to our iOS mobile applications. All without any of the repetitive code required when calling StoreKit directly.
Working with Product Identifiers
By decorating our Product Identifiers with specific keywords, we can have Xamarin.InAppPuchase automatically handle things such as the product type, subscription duration, consumable quantities and downloading of hosted content from the iTunes App Store.
For example, we can use product.nonconsumable to define a non-consumable product, gold.coins.consumable_x25 a consumable package of 25 gold coins, magazine.subscription.duration1month an auto renewing monthly subscription and antivirus.nonrenewingsubscription.duration6months a non-renewing six month subscription.
So let’s take a look at how easy it is to get a list of available In App Products from the iTunes App Store with Xamarin.InAppPurchase by using the following code:
using Xamarin.InAppPurchase; using Xamarin.InAppPurchase.Utilities; ... public InAppPurchaseManager PurchaseManager = new InAppPurchaseManager (); ... // Ask the iTunes App Store to return information about available In App Products for sale PurchaseManager.QueryInventory (new string[] { "product.nonconsumable", "gold.coins.consumable_x25", "newsletter.freesubscription", "magazine.subscription.duration1month", "antivirus.nonrenewingsubscription.duration6months", "content.nonconsumable.downloadable", });
After this code runs, the PurchaseManager will contain a list of any valid products with all of the localized information read from the iTunes App Store, ready for us to display to the end user.
When the user is ready to buy a given item, we can use the following code to start the purchase process with the iTunes App Store:
// Ask iTunes App Store to purchase product PurchaseManager.BuyProduct (Product);
We can then monitor several of the events exposed by the PurchaseManager to update the app’s UI or activate new content or features. At any time we can ask if a given product has been purchased using:
// Was the feature purchased? if (PurchaseManager.ProductPurchased("my.nonconsumabe.feature")) { ... }
In addition, Xamarin.InAppPurchase will automatically track our consumable product quantities and subscription expiration dates and provides methods to work with them easily.
Secure Automatic Persistence
The Xamarin.InAppPurchase component includes ways to automatically and securely persist the user’s purchased products. This information can be stored either to the app’s user preferences, a local file, iCloud, or our own custom persistence methods.
Now, let’s look at restoring previous purchase history that has been automatically saved by the Xamarin.InAppPurchase component. We’ll add the following code before our call to QueryInventory above:
// Setup automatic purchase persistence and load any previous purchases PurchaseManager.automaticPersistenceType = InAppPurchasePersistenceType.LocalFile; PurchaseManager.PersistenceFilename = "AtomicData"; PurchaseManager.shuffleProductsOnPersistence = false; PurchaseManager.RestoreProducts ();
With this code in place the Xamarin.InAppPurchase component automatically saves any changes to the user’s purchased products and restores that history when our iOS app starts.
Simulating the iTunes App Store
There are several situations that can arise when working with In App Purchases, many of which can be hard to test for. The Xamarin.InAppPurchase component provides the ability to simulate interaction with the iTunes App Store so we can fully test out our application. This helps to ensure and provide a smooth, issue free In App Purchase experience for our users.
When running in the simulation mode, decorate your product identifiers with specific keywords to test such things as invalid product IDs, failed purchases, hosted content download, etc. You can even test products before they are added to iTunes Connect and can test In App Purchases inside the iOS Simulator on a Mac.
Let’s look at how easy it is to run the Xamarin.InAppPurchase component in the simulation mode. We’ll modify our code above to look like the following:
// Initialize the purchase manager PurchaseManager.simulateiTunesAppStore = true; ... // Ask the iTunes App Store to return information about available In App Products for sale PurchaseManager.QueryInventory (new string[] { "product.nonconsumable", "feature.nonconsumable", "feature.nonconsumable.fail", "gold.coins.consumable_x25", "gold.coins.consumable_x50", "gold.coins.consumable_x100", "newsletter.freesubscription", "magazine.subscription.duration1month", "antivirus.nonrenewingsubscription.duration6months", "antivirus.nonrenewingsubscription.duration1year", "product.nonconsumable.invalid", "content.nonconsumable.downloadable", "content.nonconsumable.downloadfail", "content.nonconsumable.downloadupdate" }); ... // Setup the list of simulated purchases to restore when doing a simulated restore of purchases // from the iTunes App Store PurchaseManager.simulatedRestoredPurchaseProducts = "product.nonconsumable,antivirus.nonrenewingsubscription.duration6months,content.nonconsumable.downloadable";
Now we can run our iOS mobile app and test interactions such as the purchasing of a product failing, downloading of hosted content failing halfway through, or any other event that can happen with In App Purchases.
For more details about the Xamarin.InAppPurchase component and to get a full demo iOS application built using it on the Xamarin Component Store.
0 comments