Skip to content

A purhcase kit using StoreKit2 to replace RevenueCat.

License

Notifications You must be signed in to change notification settings

SpaceGrey/SGPurchaseKit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SGPurchaseKit

Swift SPM Platforms

SGPurchaseKit is a simple replacement for RevenueCat using StoreKit2 with the same calling style and fallback support.

Requirements

  • iOS 15+
  • macOS 12+
  • tvOS 15+
  • watchOS 8+

Installation

  1. In Xcode, open the project that you want to add this package.
  2. From the menu bar, select File > Swift Packages > Add Package Dependency...
  3. Paste the URL for this repository into the search field.
  4. Select the SGPurchaseKit Library.
  5. Follow the prompts for adding the package.

Quick Start

Create Your Products Plist

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.

Init Your Products

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
        

Check Purchase Status

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.
}

Display Your Products on Paywall

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")

Make a Purchase

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.

Restore

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")

Fallback Policy

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()

About

A purhcase kit using StoreKit2 to replace RevenueCat.

Resources

License

Stars

Watchers

Forks

Languages