Go back

Adding In-App Purchase on Exported hyperPad Projects

Hey there, hyperPad users! 🚀 Here’s how you can take exported projects to the next level. Let us dive into how you can implement In-App Purchases (IAP) for your exported hyperPad project!

We've got your back every step of the way. In this tutorial, we'll show you how to seamlessly integrate IAP into your hyperPad creations. This will allow you to monetize your hard work and unlock a whole new level of possibilities.

Buckle up, because your exported projects are about to become not just awesome, but profit-boosting too! 💰

PS Please check all the requirements below before proceeding.

Requirements

  • You need to have a Mac computer to export your project and add in-app purchases to your exported project.
  • You need to have cocoapods installed on the mac. If you don't have cocoapods installed on your Mac, keep scrolling!
  • You need to have the Terminal app installed on your Mac.
  • You need a text editor app installed on your Mac.

Cocoapods

CocoaPods serves as a manager for library dependencies in your Xcode projects. These dependencies are outlined in a designated text file named a Podfile. CocoaPods takes charge of resolving dependencies among libraries, fetching the corresponding source code, and subsequently linking everything within an Xcode workspace to compile your project.

The primary objective is to enhance the discoverability and interaction with third-party open-source libraries by establishing a more centralized ecosystem.

Learn more here.

Terminal App

The Terminal app on macOS is a command-line interface that allows users to interact with their computer through text-based commands. It provides a direct way to control the system, execute tasks, navigate files and directories, and perform various operations. 

Users can access powerful tools and utilities, run scripts, and configure system settings by typing commands. The Terminal offers a level of control and customization beyond what graphical interfaces provide, making it a valuable tool for developers, system administrators, and users who prefer a command-line approach to computing on macOS.

Procedure

  1. Add pod 'SwiftyStoreKit' to the list of pods in the Podfile file of your exported project using a text editor.
  2. In the terminal, run pod install. This might take a while, so hang on!

Replace the contents of HPSwift.swift with the following file

//

//  HPSwift.swift

//  hyperPad-Project

//

//  Created by Hamed Saadat on 2019-08-07.

//

import Foundation

import SwiftyStoreKit

@objc class HPSwift: NSObject {    fileprivate var _behaviours: HPBehaviours? = nil    @objc var behaviours: HPBehaviours? {        get {            return _behaviours        }

   set(behaviours) {
       _behaviours = behaviours

       SwiftyStoreKit.completeTransactions(atomically: true) { purchases in
           for purchase in purchases {
               switch purchase.transaction.transactionState {
               case .purchased, .restored:
                   if purchase.needsFinishTransaction {
                       // Deliver content from server, then:
                       SwiftyStoreKit.finishTransaction(purchase.transaction)
                   }
                   // Unlock content
                   behaviours?.broadcastValue(purchase.productId as NSString, withKey: "purchaseComplete")

case .failed, .purchasing, .deferred:
                   behaviours?.broadcastValue(purchase.productId as NSString, withKey: "purchaseError")
                   break // do nothing
               @unknown default:
                   break
               }
           }
       }

       behaviours?.addReceiveKey("restorePurchases", onReceive: { (receiveValue) in
           SwiftyStoreKit.restorePurchases(atomically: true) { results in
               if results.restoreFailedPurchases.count > 0 {
                   behaviours?.broadcastValue("Restore Failed: \(results.restoreFailedPurchases)" as NSString, withKey: "purchaseError")
               }
               else if results.restoredPurchases.count > 0 {
                   results.restoredPurchases.forEach { (purchase) in
                       behaviours?.broadcastValue(purchase.productId as NSString, withKey: "purchaseComplete")
                   }
               }
               else {
                   print("Nothing to Restore")
               }
           }
       })

       /*
        Add Behaviour functionality here
       */

       behaviours?.addReceiveKey("purchase", onReceive: { (recieveValue) in
            // handle in app purchase logic! //

SwiftyStoreKit.purchaseProduct("INSERT_PURCHASE_IDENTIFIER_HERE_OR_USE_recieveValue", quantity: 1, atomically: true) { result in

                        switch result {

                        case .success(let purchase):

                            behaviours?.broadcastValue(purchase.productId as NSString, withKey: "purchaseComplete")

                        case .error(let error):

                            var message = ""

                            switch error.code {

                            case .unknown: message = "Unknown error. Please contact support"

                            case .clientInvalid: message = "Not allowed to make the payment"

                            case .paymentCancelled: break

                            case .paymentInvalid: message = "The purchase identifier was invalid"

                            case .paymentNotAllowed: message = "The device is not allowed to make the payment"

                            case .storeProductNotAvailable: message = "The product is not available in the current storefront"

                            case .cloudServicePermissionDenied: message = "Access to cloud service information is not allowed"

                            case .cloudServiceNetworkConnectionFailed: message = "Could not connect to the network"

                            case .cloudServiceRevoked: message = "User has revoked permission to use this cloud service"

                            default: message = ((error as NSError).localizedDescription)

                            }

                            behaviours?.broadcastValue(message as NSString, withKey: "purchaseFailed")

                        }

                    }

            })

        }

    }

Things to Note

behaviours?.addReceiveKey("restorePurchases", onReceive: { (receiveValue) in

                // add your code here

            })

This will get triggered when you trigger a broadcast behavior with the restorePurchases key.

behaviours?.broadcastValue(purchase.productId as NSString, withKey: "purchaseComplete")

Once the purchase is complete, it will trigger receive message behaviors with the purchaseComplete key. INSERT_PURCHASE_IDENTIFIER_HERE_OR_USE_recieveValue can be replaced with either the identifier of your purchase from AppStoreConnect OR the receiveValue parameter. If you use receiveValue make sure in your broadcast behavior, you're sending the identifier from App Store Connect.

Yay!

And you’re now done! Tag us with your completed and uploaded hyperPad project so we can reshare your hyperPad creations with the community! 🚀✨

If you’ve had more than one (1) hyperPad made app that’s published - you could be our next hyperPad Ambassador! Full of perks and opportunities, sign up to be a hyperPad Ambassador here.

Stay up to date

Enter your email to learn about great updates, contests, and new features

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

recent posts

See all posts