Time for action – testing the In-App Purchase with the Breakout In-App Purchase Demo
Before you can test an In-App Purchase on an iOS device, make sure that you have a test user account in iTunes Connect. Also, make sure that you've created a distribution build using an ad hoc Distribution Provisioning Profile for the app to test In-App Purchase's features. If you followed all the earlier steps in this chapter, testing a purchase through the store will work accordingly:
- In the Corona simulator, create a distribution build of the Breakout In-App Purchase Demo. Once the build has been compiled, upload the build on your iOS device.
- Keep your device connected to your machine and launch Xcode. From the toolbar, go to Windows | Organizer. Once you're in Organizer, select the device that is connected in the Devices section and then select Console. This will allow you to check the console output of your device to catch debug messages from your code (that is, print statements) and any application crashes.
- Before launching the application, you'll need to select the Settings icon on your device. Scroll up until you see the Store icon and select it.
- Sign out of your iTunes Store account if you're logged in, so that you can test In-App Purchases in the sandbox environment.
- Launch the Breakout In-App Purchase Demo from your device. Select the Play button and then select the Locked button. The screen will transition back to the main menu, and a window will pop up to confirm your In-App Purchase. Press OK to continue with the purchase.
- Next, you will be greeted with another window to sign in with your Apple ID. This is where you will log in with the test user account you created in iTunes Connect. Do not sign in with your actual Apple account that was used to log in to iTunes Connect.
- Once you've logged in, select the Play button again. You will notice that the 2 button has been unlocked. When you select it, you will have access to that scene.
- Exit the app and refer to the console. You will notice the output from the device and some familiar print statements from your code. The console log displays the Product ID used for the In-App Purchase and informs you whether it is valid and whether the transaction is successful.
- If you want to ensure that the In-App Purchase actually worked, delete the application from your device and log out of your user test account. Upload the same build on your device—there's no need to create a new one. Launch the application and run the In-App Purchase again. Log in using the same user test account. You should receive a pop-up window that mentions that you've already purchased the product, and asks you whether you want to download it again for free. Receiving a notification means your In-App Purchase was successful.
It is important to follow the In-App Purchase test steps properly. To make sure you're getting accurate results in the sandbox environment, signing out of your Apple account from the Store settings is the key to this entire process.
Once you launch the application and call the store function by pressing the Locked button, you will notice the display name and price of the In-App Purchase. It should match with what you created in iTunes Connect, if you have implemented it correctly.
When you log in by using the test user account you created in iTunes Connect, the transaction should go through without any errors, assuming that there are no server issues on Apple's side or connection problems on the device. Level 2 on the level select screen will be unlocked and accessible. Congratulations! You have created an In-App Purchase.
Have a go hero – using other In-App Purchase types
In the Breakout In-App Purchase Demo, we focused more on nonconsumable In-App Purchases. Try integrating consumable, auto-renewing, or non-renewing subscriptions with your own apps.
Apps that feature consumable products are games that require currency to buy or build things in a free-to-play environment. Subscription products can be focused towards games that are never-ending and are constantly updated with new levels or games that may require an online server to interact in a multiplayer environment. See what you can come up with!
Pop quiz – all about In-App Purchases
Q1. What are nonconsumable purchases?
- Products that only need to be purchased once by the user.
- Products that need to be purchased each time the user needs the item.
- Products that allow the user to purchase content for a set duration of time.
- A subscription that requires a user to renew each time it expires.
Q2. What is true about testing In-App Purchases?
- You need to be logged in to your account at all times.
- Your Apple account is used to test In-App Purchases.
- Login to your user test account when prompted in In-App Purchase sandbox.
- None of the above.
Q3. What type of Provisioning Profile must be used to test In-App Purchases?
- Development Provisioning Profile.
- Ad Hoc Distribution Provisioning Profile.
- App Store Distribution Provisioning Profile.
- None of the above.