A lightweight wrapper for Apple's StoreKit, written in Swift.
Add pod 'StoreKitCompanion' to your Podfile and run pod install.
Add github "Recisio/StoreKitCompanion" to your Cartfile.
StoreKitCompanion is available on SPM. Just add the following to your Package file:
import PackageDescription
letpackage=Package(
dependencies:[.Package(url:"https://github.com/recisio/StoreKitCompanion.git", majorVersion:1)])Add StoreKitCompanion.swift to your project.
StoreKitCompanion lets you fetch products, launch payment transactions and send App Store Receipt data over the network for validation in a couple of lines of code.
Fetching products is easy, call fetchProductsWithIdentifiers(_:completion:) and pass in a set of product identifiers.
At a very minimum, also supply a completion closure to get the valid SKProduct objects returned.
Optionally, supply a failure closure to handle errors.
StoreKitCompanion.sharedInstance.fetchProductsWithIdentifiers(["com.mycompany.MyKillerProduct"], completion:{ products, invalids inprint("Got products", products)print("Got invalids", invalids)})Before trying to buy something, you should check whether users are allowed to make payments.
You can do so by calling canMakePayments().
StoreKitCompanion.sharedInstance.canMakePayments()Payments are submitted to the default payment queue (SKPaymentQueue.defaultQueue()) and StoreKitCompanion observes it.
Whenever something happen on it, StoreKitCompanion sends notifications, and calls closures that you set.
// Provide a closure to be called when transactions are updated
StoreKitCompanion.sharedInstance.transactionsUpdateHandler ={ queue, transactions in
// Process transactions..
}
// Add a payment, quantity is 1 by default
StoreKitCompanion.sharedInstance.addPaymentForProductIdentifier("com.mycompany.MyKillerProduct")You can restore completed transactions by calling restoreCompletedTransactionsWithUsername(_:).
You may also provide an optional username.
Callbacks that you set are called in response to the restoration request.
StoreKitCompanion.restoreCompletedTransactionsWithUsername()Details about App Store receipt validation can be found on Apple's website. While StoreKitCompanion doesn't perform any local validation (yet?) of the receipt, it let's you send it to a server for validation.
By default, StoreKitCompanion will send the data using a HTTP POST request, with a parameter named receiptData, but you can override those settings if you need to.
// First supply a validation URL, StoreKitCompanion will use it
StoreKitCompanion.sharedInstance.validationURLString ="http://myserver.com"
// Send the data out
StoreKitCompanion.sharedInstance.sendReceiptWithPOSTRequest(){ responseData, error inprint("Got response", responseData)print("Got error", error)}To customize the way the receipt is sent, use sendReceiptWithDescriptor(_:completion:) and provide it with any object that adopts the ReceiptValidationRequestDescriptor protocol.
Default values are already provided in a protocol extension, so override only what's needed.
structMyRequestDescriptor:ReceiptValidationRequestDescriptor{
// Name parameters differently or add parameters
func parametersWithReceiptData(receiptData:NSData)->[String:AnyObject]?{return["best_param_name": receiptData,"new_param":"new_param_value"]}}
// Send the data out
StoreKitCompanion.sharedInstance.sendReceiptWithDescriptor(MyRequestDescriptor()){ responseData, error inprint("Got response", responseData)print("Got error", error)}StoreKitCompanion watches the default payment queue and sends notifications :
/**
The name of the notification sent when the payment queue finished to restore completed transactions.
User info dictionary contains a reference to the `SKPaymentQueue` object.
*/
publicstaticletPaymentQueueDidFinishRestoringCompletedTransactions="SKCPaymentQueueDidFinishRestoringCompletedTransactions"
/**
The name of the notification sent when transactions are updated.
User info dictionary contains a reference to the `SKPaymentQueue` object and an array of `SKPaymentTransaction` objects.
*/
publicstaticletPaymentQueueDidUpdateTransactions="SKCPaymentQueueDidUpdateTransactions"
/**
The name of the notification sent when transactions are removed.
User info dictionary contains a reference to the `SKPaymentQueue` object and an array of `SKPaymentTransaction` objects.
*/
publicstaticletPaymentQueueDidRemoveTransactions="SKCPaymentQueueDidRemoveTransactions"
/**
The name of the notification sent when downloads are updated
User info dictionary contains a reference to the `SKPaymentQueue` object and an array of `SKDownload` objects.
*/
publicstaticletPaymentQueueDidUpdateDownloads="SKCPaymentQueueDidUpdateDownloads"
/**
The name of the notification sent when the payment queue fails to restore completed transactions.
User info dictionary contains a reference to the `SKPaymentQueue` object and a reference to the `NSError` object.
*/
publicstaticletPaymentQueueDidFailRestoringCompletedTransactions="SKCPaymentQueueDidFailRestoringCompletedTransactions"Another way to handle payment queue events is to provide StoreKitCompanion with closures for those events :
publictypealiasCompletedTransactionsRestoreCompletion=(SKPaymentQueue)->VoidpublictypealiasCompletedTransactionsRestoreFailure=(SKPaymentQueue,NSError)->VoidpublictypealiasDownloadsUpdateCompletion=(SKPaymentQueue,[SKDownload])->VoidpublictypealiasTransactionsUpdateCompletion=(SKPaymentQueue,[SKPaymentTransaction])->VoidpublictypealiasTransactionsRemovalCompletion=(SKPaymentQueue,[SKPaymentTransaction])->Void
// ....
/**
Handles successful completed transactions restoration
*/
publicvarcompletedTransactionsRestorationSuccessHandler:CompletedTransactionsRestoreCompletion?
/**
Handles completed transaction restoration failure
*/
publicvarcompletedTransactionsRestorationFailureHandler:CompletedTransactionsRestoreFailure?
/**
Handles successful downloads restoration
*/
publicvardownloadsUpdateSuccessHandler:DownloadsUpdateCompletion?
/**
Handles transaction updates
*/
publicvartransactionsUpdateHandler:TransactionsUpdateCompletion?
/**
Handles transaction removals
*/
publicvartransactionsRemovalHandler:TransactionsRemovalCompletion?- Handling transaction restoration
- Validating App Store Receipt locally
- If you found a bug, open an issue
- If you have a feature request, open an issue
- If you want to contribute, submit a pull request
StoreKitCompanion is available under the MIT license. See the LICENSE file for more info.