Store Manager for SwiftUI and In-App Purchases
GOOStore is a Library prepared to use In-App Purchases in iOS
and macOS
developments with SwiftUI
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.50livesNon-Consumable
- for example dev.goojoob.MyApp.ProUser
- In Xcode select File > Add Packages.
- Enter this project's URL: https://github.com/goojoob/GOOStore.git
Declare your available products id's and quantity as a Dictionary.
- Consumable items need a quantity you may treat when the purchase is done
- Non-Consumable items don't need a quantity, so you may use a number to identify it
let myProducts: [String: Int] = ["dev.goojoob.MyApp.10lives": 10, "dev.goojoob.MyApp.50lives": 50, "dev.goojoob.MyApp.ProUser": 999]
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:
- Variable lastBuy is a tuple containing the las item purchased, its productId and quantity
.onChange(of: storeManager.transactionState) { transactionState in
switch transactionState {
case .purchased:
print("StoreManager - Purchased: '\(storeManager.lastBuy.productId)' Quantity: \(storeManager.lastBuy.quantity)")
//treat your purchase
case .restored:
//treat your restored purchases
default:
break
}
}
- macOS 10.15+
- iOS 13.0+
Goojoob.dev - Original development - goojoob
This work is licensed under a Creative Commons Attribution 4.0 International license (CC BY 4.0).
- Talk to others about this project 📢
- We can have a ☕ whenever you want