Demo of a simple in-app purchase controller. The controller performs only basic functions:
- Request product information from the App Store
- Buy a product
- Restore purchases.
- Guides and Sample Code. In-App Purchase Programming Guide
- API Reference. StoreKit.
- WWDC 2016. Session 702
- Technical Note TN2387. In-App Purchase Best Practices
I'd add an instance of AppPurchaseController
to the application delegate (app/flow coordinator):
lazy var purchase: PurchaseController = {
let controller = AppPurchaseController(identifiers: self.productIdentifiers)
controller.converter = AppPriceConverter()
controller.observer = AppPaymentTransactionObserver()
controller.requester = AppProductRequester()
controller.products = AppProductStore()
return controller
}()
It is recommended to request the product information when the app starts up (becomes active? enter to the foreground). For example, so:
- (void)applicationDidBecomeActive:(UIApplication *)application {
_ = self.productInfoUpdater.updateIfNeeded()
}
where `` is lazy property:
lazy var productInfoUpdater: ProductInfoUpdater = {
let updater = AppProductInfoUpdater(purchaseController: self.purchase,
timeout: Timeouts.productInfoUpdateTimeInterval)
return updater
}()
The source code if in Swift and is fully testable. Hopefully, I will add more unit tests and more complicated features later.