SwiftyStoreKit
Lightweight In App Purchases Swift framework for iOS 8.0+, tvOS 9.0+ and macOS 10.10+ ⛺
Install / Use
/learn @bizz84/SwiftyStoreKitREADME
SwiftyStoreKit is a lightweight In App Purchases framework for iOS, tvOS, watchOS, macOS, and Mac Catalyst.
Features
- Super easy-to-use block-based API
- Support for consumable and non-consumable in-app purchases
- Support for free, auto-renewable and non-renewing subscriptions
- Support for in-app purchases started in the App Store (iOS 11)
- Support for subscription discounts and offers
- Remote receipt verification
- Verify purchases, subscriptions, subscription groups
- Downloading content hosted with Apple
- iOS, tvOS, watchOS, macOS, and Catalyst compatible
SwiftyStoreKit Alternatives
During WWDC21, Apple introduced StoreKit 2, a brand new Swift API for in-app purchases and auto-renewable subscriptions.
While it would be highly desirable to support StoreKit 2 in this project, little progress has been made over the last year and most issues remain unanswered.
Fortunately, there are some very good alternatives to SwiftyStoreKit, backed by real companies. By choosing their products, you'll make a safe choice and get much better support.
RevenueCat
RevenueCat is a great alternative to SwiftyStoreKit, offering great APIs, support, and much more at a very reasonable price.
If you've been using SwiftyStoreKit and want to migrate to RevenueCat, this guide covers everything you need:
Or if you're just getting started, consider skipping SwiftyStoreKit altogether and signing up for RevenueCat.
Glassfy
Glassfy makes it easy to build, handle, and optimize in-app subscriptions. If you switch to Glassfy from SwiftyStoreKit, you'll get a 20% discount by using this affiliate link.
- Glassfy pricing page - 20% off
- Glassfy migration guide to support you with the migration
Note from the author: if you sign up with the link above, I will receive an affiliate commission from Glassfy, at no cost to yourself. I only recommend products that I personally know and believe will help you.
Apphud
Apphud is more than just making a purchase and validating receipts. Apphud is all-in-one infrastructure for your app growth. Sign up for free and try it out.
Or you can learn how to migrate your app from SwiftyStoreKit to Apphud.
Adapty
With Adapty you can set up subscriptions in just an hour following these simple steps and instantly launch in-app purchases with the Paywall Builder. Adapty not only gives you the tools to embed purchases but also helps your customers grow. And the best part is that it’s free for apps <$10k.
Contributions Wanted
SwiftyStoreKit makes it easy for an incredible number of developers to seemlessly integrate in-App Purchases. This project, however, is now community-led. We need help building out features and writing tests (see issue #550).
Maintainers Wanted
The author is no longer maintaining this project actively. If you'd like to become a maintainer, join the Slack workspace and enter the #maintainers channel. Going forward, SwiftyStoreKit should be made for the community, by the community.
More info here: The Future of SwiftyStoreKit: Maintainers Wanted.
Requirements
If you've shipped an app in the last five years, you're probably good to go. Some features (like discounts) are only available on new OS versions, but most features are available as far back as:
| iOS | watchOS | tvOS | macOS | Mac Catalyst | | --- | ------- | ---- | ----- | ------------ | | 8.0 | 6.2 | 9.0 | 10.10 | 13.0 |
Installation
There are a number of ways to install SwiftyStoreKit for your project. Swift Package Manager, CocoaPods, and Carthage integrations are the preferred and recommended approaches.
Regardless, make sure to import the project wherever you may use it:
import SwiftyStoreKit
Swift Package Manager
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into Xcode and the Swift compiler. This is the recommended installation method. Updates to SwiftyStoreKit will always be available immediately to projects with SPM. SPM is also integrated directly with Xcode.
If you are using Xcode 11 or later:
- Click
File Swift PackagesAdd Package Dependency...- Specify the git URL for SwiftyStoreKit.
https://github.com/bizz84/SwiftyStoreKit.git
Carthage
To integrate SwiftyStoreKit into your Xcode project using Carthage, specify it in your Cartfile:
github "bizz84/SwiftyStoreKit"
NOTE: Please ensure that you have the latest Carthage installed.
CocoaPods
SwiftyStoreKit can be installed as a CocoaPod and builds as a Swift framework. To install, include this in your Podfile.
use_frameworks!
pod 'SwiftyStoreKit'
Contributing
Got issues / pull requests / want to contribute? Read here.
Documentation
Full documentation is available on the SwiftyStoreKit Wiki. As SwiftyStoreKit (and Apple's StoreKit) gains features, platforms, and implementation approaches, new information will be added to the Wiki. Essential documentation is available here in the README and should be enough to get you up and running.
App startup
Complete Transactions
Apple recommends to register a transaction observer as soon as the app starts:
Adding your app's observer at launch ensures that it will persist during all launches of your app, thus allowing your app to receive all the payment queue notifications.
SwiftyStoreKit supports this by calling completeTransactions() when the app starts:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// see notes below for the meaning of Atomic / Non-Atomic
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
case .failed, .purchasing, .deferred:
break // do nothing
}
}
}
return true
}
If there are any pending transactions at this point, these will be reported by the completion block so that the app state and UI can be updated.
If there are no pending transactions, the completion block will not be called.
Note that completeTransactions() should only be called once in your code, in application(:didFinishLaunchingWithOptions:).
Purchases
Retrieve products info
SwiftyStoreKit.retrieveProductsInfo(["com.musevisions.SwiftyStoreKit.Purchase1"]) { result in
if let product = result.retrievedProducts.first {
let priceString = product.localizedPrice!
print("Product: \(product.localizedDescription), price: \(priceString)")
}
else if let invalidProductId = result.invalidProductIDs.first {
print("Invalid product identifier: \(invalidProductId)")
}
else {
print("Error: \(result.error)")
}
}
Purchase a product (given a product id)
- Atomic: to be used when the content is delivered immediately.
SwiftyStoreKit.purchaseProduct("com.musevisions.SwiftyStoreKit.Purchase1", quantity: 1, atomically: true) { result in
switch result {
case .success(let purchase):
print("Purchase Success: \(purchase.productId)")
case .error(let error):
switch error.code {
case .unknown: print("Unknown error. Please contact support")
case .clientInvalid: print("Not
