SwiftyStoreKit is a lightweight In App Purchases framework for iOS 8.0+ and OSX 9.0+, written in Swift.
[](http://mit-license.org)
[](https://developer.apple.com/resources/)
[](https://developer.apple.com/swift)
[](https://github.com/bizz84/SwiftyStoreKit/issues)
SwiftyStoreKit.retrieveProductsInfo(["com.musevisions.SwiftyStoreKit.Purchase1"]) { result in
if let product = result.retrievedProducts.first {
let priceString = NSNumberFormatter.localizedStringFromNumber(product.price ?? 0, numberStyle: .CurrencyStyle)
print("Product: \(product.localizedDescription), price: \(priceString)")
}
else if let invalidProductId = result.invalidProductIDs.first {
return alertWithTitle("Could not retrieve product info", message: "Invalid product identifier: \(invalidProductId)")
}
else {
print("Error: \(result.error)")
}
}
SwiftyStoreKit.purchaseProduct("com.musevisions.SwiftyStoreKit.Purchase1") { result in
switch result {
case .Success(let productId):
print("Purchase Success: \(productId)")
case .Error(let error):
print("Purchase Failed: \(error)")
}
}
SwiftyStoreKit.restorePurchases() { results in
if results.restoreFailedProducts.count > 0 {
print("Restore Failed: \(results.restoreFailedProducts)")
}
else if results.restoredProductIds.count > 0 {
print("Restore Success: \(results.restoredProductIds)")
}
else {
print("Nothing to Restore")
}
}
SwiftyStoreKit.verifyReceipt() { result in
if case .Error(let error) = result {
if case .NoReceiptData = error {
self.refreshReceipt()
}
}
}
func refreshReceipt() {
SwiftyStoreKit.refreshReceipt { result in
switch result {
case .Success:
print("Receipt refresh success")
case .Error(let error):
print("Receipt refresh failed: \(error)")
}
}
}
NOTE: The framework provides a simple block based API with robust error handling on top of the existing StoreKit framework. It does NOT persist in app purchases data locally. It is up to clients to do this with a storage solution of choice (i.e. NSUserDefaults, CoreData, Keychain).
SwiftyStoreKit can be installed as a CocoaPod and builds as a Swift framework. To install, include this in your Podfile.
use_frameworks!
pod 'SwiftyStoreKit'
Once installed, just import SwiftyStoreKit
in your classes and you're good to go.
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.
The project includes demo apps for iOS and OSX showing how to use SwiftyStoreKit. Note that the pre-registered in app purchases in the demo apps are for illustration purposes only and may not work as iTunes Connect may invalidate them.
retrieveProductsInfo()
API call, which takes a set of product IDs and returns a struct with information about the corresponding SKProducts. Related issue #21restorePurchases()
completion closure has been changed to return all restored purchases in one call. Related issue #18In order to make a purchase, two operations are needed:
Obtain the SKProduct
corresponding to the productId that identifies the app purchase, via SKProductRequest
.
Submit the payment for that product via SKPaymentQueue
.
The framework takes care of caching SKProducts so that future requests for the same SKProduct
don't need to perform a new SKProductRequest
.
SwiftyStoreKit wraps the delegate-based SKProductRequest
API with a block based class named InAppProductQueryRequest
, which returns a RetrieveResults
value with information about the obtained products:
public struct RetrieveResults {
public let retrievedProducts: Set<SKProduct>
public let invalidProductIDs: Set<String>
public let error: NSError?
}
This value is then surfaced back to the caller of the retrieveProductsInfo()
method the completion closure so that the client can update accordingly.
InAppProductPurchaseRequest
is a wrapper class for SKPaymentQueue
that can be used to purchase a product or restore purchases.
The class conforms to the SKPaymentTransactionObserver
protocol in order to receive transactions notifications from the payment queue. The following outcomes are defined for a purchase/restore action:
enum TransactionResult {
case Purchased(productId: String)
case Restored(productId: String)
case Failed(error: NSError)
}
Depending on the operation, the completion closure for InAppProductPurchaseRequest
is then mapped to either a PurchaseResult
or a RestoreResults
value and returned to the caller.
Many thanks to phimage for adding OSX support and receipt verification.
It would be great to showcase apps using SwiftyStoreKit here. Pull requests welcome :)
Copyright (c) 2015-2016 Andrea Bizzotto bizz84@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.