TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Nov 2016 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✓ |
Maintained by Paul Wilkinson.
A framework for monitoring auto renewing subscriptions on iOS
SubscriptionMonitor automates the tasks required to validate in-app purchase receipts for auto-renewing subscriptions. It will periodically refresh the application receipt and validate it against your server.
An NSNotification (and optionally a closure invocation) is used to let your app know that the receipt has been refreshed and that it should check for changes in subscriptions.
Features
SubscriptionMonitor supports iOS 9 and above. Your project must be written in Swift 3 in order to integrate SubscriptionMonitor. An external web server is required to communicate with Apple’s servers to perform receipt validation.
SubscriptionMonitor is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "SubscriptionMonitor"
Using SubscriptionManager
is straight-forward:
ReceiptValidator
- SimpleReceiptValidator
works with the sample php
script (see below)let validator = SimpleReceiptValidator(serverBase: "https://yourserver.yourdomain.com/iTunesReceiptValidator.php",
targetBundle:"com.yourdomain.yourapp")
SubscriptionMonitor
that uses the validator - this needs to held where it won’t be released, such as a property of your UIApplicationDelegate
classself.subscriptionMonitor = SubscriptionMonitor(validator: validator,
refreshInterval: 3600, useSandbox: false)
ProductGroup
contains a free product, then the free product will be 'active’ when there are no other active subscriptions in that product group.let productGroup = ProductGroup(name: "First Product Group")
let product1 = Product(productID: "com.mydomain.myProduct1", productLevel: 1, duration: .year)
let product2 = Product(productID: "com.mydomain.myProduct2", productLevel: 1, duration: .month)
let freeProduct = FreeProduct(productID: "com.mydomain.freeproduct", productLevel: 99)
productGroup.add(product: product1)
productGroup.add(product: product2)
productGroup.add(product: FreeProduct)
self.subscriptionMonitor.add(productGroup: productGroup)
self.subscriptionMonitor.setUpdateCallback { (receipt, subscriptions, error) -> Void in
if error != nil {
print("There was an error: \(error)")
}
// Note that even after an error there may be active `subscriptions` if you have free products defined
for subscription in subscriptions {
print("Active product: \(subscription.product.productID)")
}
}
You can also subscribe to the SubscriptionMonitorRefreshNotification
NSNotification
. The userInfo
for this notification may contain keys for “Error”, “Active” and “Receipt” depending on the validation result.
Call startRefreshing
to start the time-based refreshing of receipt and subscription information:
self.subscriptionMonitor.startRefreshing()
refreshNow
self.subscriptionMonitor.refreshNow()
Apple advises that you should use a server to provide an interface between your app and their receipt validation server as this allows you to build additional levels of security and trust into the process. The SimpleReceiptValidator
class that is included with SubscriptionMonitor is written to work with the iTunesReceiptValidator.php
script that can be found in the php directory in the repo. This script needs to be modified to contain the shared secret that can be retrieved from iTunesConnect.
paulw, [email protected]
SubscriptionMonitor is available under the MIT license. See the LICENSE file for more info.