SGPurchaseKit is a simple replacement for RevenueCat using StoreKit2 with the same calling style and fallback support.
- iOS 15+
- macOS 12+
- tvOS 15+
- watchOS 8+
- In Xcode, open the project that you want to add this package.
- From the menu bar, select File > Swift Packages > Add Package Dependency...
- Paste the URL for this repository into the search field.
- Select the
SGPurchaseKit
Library. - Follow the prompts for adding the package.
SGPurchase can group your multiple products into groups, and you can directly retrieve the group purchase status.
Firstly you need to create your .plist
in your target.
The structure is like:
<plist version="1.0">
<dict>
<key>PurchaseGroup1</key>
<array>
<string>com.item1.year</string>
<string>com.item1.lifetime</string>
</array>
<key>PurchaseGroup2</key>
<array>
<string>com.item2.month</string>
</array>
</dict>
</plist>
It will be decoded into [String:[String]]
dictionary.
In your app's UIApplication Delegate or init of App
struct in SwiftUI, call initItems
and set other parameters.
let purchaseListURL = Bundle.main.url(forResource: "PurchaseItems", withExtension: "plist")!
SGPurchases.initItems(from: purchaseListURL)
SGPurchases.fallbackPolicy = .days(4)
SGPurchases.enableLog = true
If you want to check the purchase status of a certain group, call SGPurchases.shared.checkGroupStatus
:
if await SGPurchases.shared.checkGroupStatus("PurchaseGroup1"){
//give the user paid content.
} else {
//remove content.
}
If you want to get products for certain groups, call SGPurchases.shared.getProducts
, it returns a SGProduct
array that contains the StoreKit product. It's sorted by price from low to high by default.
packages = await SGPurchases.shared.getProducts("PurchaseGroup1")
Call SGPurchases.shared.purchase
and pass the selected SGProduct
let transaction = try? await SGPurchases.shared.purchase(c)
let result = await SGPurchases.shared.checkGroupStatus("PurchaseGroup1")//check the group status after purchase.
Call SGPurchases.shared.restorePurchases
. You don't need to call restore when you launch the app, the SGPurchaseKit
automatically listens to the remote transactions and updates the status.
await SGPurchases.shared.restorePurchase()
let result = await SGPurchases.shared.checkGroupStatus("PurchaseGroup1")
In some situations, like user is offline, or the user switches to a different account. You can choose whether to keep the user's access using cache for specific days, or simply disable the paid content.
Set it together with the initItems()