Store Manager for SwiftUI and In-App Purchases

GOOStore

Store Manager for SwiftUI and In-App Purchases

About GOOStore ?

GOOStore is a Library prepared to use In-App Purchases in iOS and macOS developments

Installation ⚙️

Setup your XCode Project and App Store Connect

First of all you need to add In-App Purchases capabilities in your app

  • In Xcode select your Target and change to the Signing & Capabilites tab.
  • Add In-App Purchase capability.

Add your In-App Purchases in App Store Connect

  • Consumable purchases – for example dev.goojoob.MyApp.10lives and dev.goojoob.MyApp.50lives
  • Non-Consumable – for example dev.goojoob.MyApp.ProUser

Setup with Swift Package Manager

Usage ?

Declare your available products id’s and quantity.

  • Consumable items need a quantity you may treat when the purchase is done
  • Non-Consumable items don’t need a quantity, so you may leave it at 0
let myProducts: [String: Int] = ["dev.goojoob.MyApp.10lives": 10, "dev.goojoob.MyApp.50lives": 50, "dev.goojoob.MyApp.ProUser": 0]

Create a StateObject var in your View with your products:

import GOOStore

//...

@StateObject private var storeManager: GOOStore = GOOStore(products: myProducts)

Show your products in a View (this design is up to you):

ForEach(storeManager
    .myProducts
    .sorted(by: { $0.price.floatValue < $1.price.floatValue }), id: \.self) { product in
        HStack {
            VStack(alignment: .leading) {
                Text(product.localizedTitle)
                Text(product.localizedDescription)
            }

            Spacer()

            Button {
                storeManager.purchaseProduct(product: product)
            } label: {
                HStack {
                    Text("\(product.localizedPrice ?? "00")")
                    Image(systemName: "creditcard")
                }
            }
        }
    }

Let Restore Purchases available (for example in the toolbar):

.toolbar {
    ToolbarItem(placement: .navigationBarTrailing) {
        Button {
            storeManager.restoreProducts()
        } label: {
            Text("Restore Purchases")
        }
    }
}

Process the purchase/restore in a View:

.onChange(of: storeManager.transactionState) { transactionState in
    switch transactionState {
    case .purchased:
        print("StoreManager - Purchased '\(storeManager.lastBuy)'")
        //treat your purchase
    case .restored:
        print("StoreManager - Restored '\(storeManager.lastBuy)'")
        //treat your restored purchases
    default:
        break
    }
}

Created with ?️

Author ✒️

Goojoob.devOriginal developmentgoojoob

License ?

This work is licensed under a Creative Commons Attribution 4.0 International license (CC BY 4.0).

Thank You ?

  • Talk to others about this project ?
  • We can have a ☕ whenever you want

GitHub

View Github