Hello, I want to share, how you can implement purchasing with IOS Native using Playmaker.
The guide will cover the way to implement:
I hope this tutorial will be useful not for only IOS Native user, but for everyone who want’s to implement In Apps in the IOS Game.
To get started you need:
First of all you need to setup your IOS app, the guide can be found here.
Now we can get started on In Apps implementation.
Note: I haven't used any global variable or events for this example. But you can feel free to use it on your own implementation to make it more elegant.
Note: Inside IOS Native The playmaker actions scripts can be found in the zip archive at: Assets/Extensions/IOSNative/Addons/PlayMakerActions.zip And the example scene with we will make in this tutorial can be founded at: Assets/Extensions/IOSNative/xExample/Scenes/PlayMaker/BillingWithPlaymakerExample
Here is how our scene will looks like.
And here is scene hierarchy view
Let’s start from the Connection implementation.
At the FSM start we will use INS_BillingInit action, with will fire the Connected and Failed actions for as.
Let also add some messages to Connected and Failed states using ISN_MessagePopUpAction action. At the beginning of connection let’s also use ISN_ShowPreloader action, and ISN_HidePreloader when connection is finished.
Here is view of init and Connected states.
Now let’s implement the our purchase buttons. We need to be able to purchase red sphere, green sphere and 100 coins pack. So let’s make 3 buttons for it.
We going to use ISN_BillingEventsListener action to listener for the billing connect event. As soon as we will have this event fired, it means that billing is ready to use and we can go to Purchase Button state, with will draw the button.
When user clicks the buttons, we will go to Purchase state with ISN_PurchaseAction.
We need to specify what product we will purchase by clicking on our button. In this case we will set our non-consumable red sphere product id.
Make sure you have setupped products in plugin settings. If you haven’t done this yet, go to Windows → IOS Native → Edit Settings and add your products ids that you have created earlier in ITunes connect
For this tutorial I have two non-consumable items:
and one consumable
Also make sure the “Send Fake Actions In Editor” option is enabled. It will give us ability to test our implementation right in the editor.
One more important thing, that on the Purchase state we not actually unlocking the product or adding the coins. In my case I will simply show the pop up:
This is made due to security reasons, for example if for some reason our app will be terminated during purchase flow, we will not provide our player with the item event he is purchased it.
So for preventing this we will create one manager with will manage all our products.
Do the same for the rest of our product buttons.
We will also implement Restore purchases button using ISN_RestorePurchases action.
The button is implemented similar to all our purchase buttons.
With ISN_RestorePurchases you can also listen for the Item Restored Event (with will be fired for each restored item) but in this example we do not need this.
So now we going to the most important part. The purchase manager. here is how it looks like:
Starting from Load Products States action we loading all our product states from the PlayerPrefs.
Variables red_sphere_item_id, green_sphere_item_id and coins_item_id are equals to iTunes ids of our products.
When our products states loaded in our local variables we can update the UI (or any other game play logic) according to our products states.
So for example here how we check and unlock greensphere product:
Check For Green Sphere State:
So we simply checking if our GreenSphereVal variable (which was loaded in Load Products States state is equals 1), and it it is, it means that our sphere is purchased and we will go to Unlock Green Sphere state.
For coins it’s a bit different, our coins amount was already loaded in ,so we need simply convert it to String and update the UI:
So far we have:
Runing purchase and restore action
Loading produc states
Update Game UI according to the product state
The only thing is left is to listened for purchase/restore events and update PlayerPrefs depending on product id.
That’s why when our product update finished we going to Listen For Billing Events state.
In this state we will use already known ISN_BillingEventsListener action, but for this time we going to litner for the Transaction Complete, and Restore Complete Actions.
When Transaction Complete it means that some product was just purchased so we need to update its state in the PlayerPrefs.
First of all we will go to Check If Coins Purchased state and check if that product was coins. We cheking it because we save our consumable item state in a but different way, point is we need to update coins amount instead of just saving purchased flag for our red and green sphere products.
So we checking current BoughtProductId with coins_item_id and if it’s equals we going to add 100 for our current coins amount and save it to PlayerPrefs:
And then we will run our product stated update cycle by returning to Load Products States states.
For Green and Red Sphere products we have same logic we just saving 1 to PlayerPrefs with the item id key:
And returning to Load Products States as well.
That’s it Consumable, Non-Consumable products with Restore Purchases ability was just implemented in our game.
Note: In the example scene I also added Clean PlayerPrefs And Restart sate, with will clean up all our PlayerPrefs and restart the scene. As imitation new device for Restore Purchases Testing.
Note: If you will test in Editor ISN_RestorePurchases will restore all non-consumable products listed in plugin setting. You should understand that it’s firing those events for testing purpose only.
On real IOS device only non-consumable items with was earlier purchased by current user will be restored. And do not forget to use iTunes test user when you testing your purchases implementation on real device.