/SimplePurchase

Demo of the in-app purchase controller

Primary LanguageSwiftApache License 2.0Apache-2.0

SimplePurchase

Demo of a simple in-app purchase controller. The controller performs only basic functions:

  1. Request product information from the App Store
  2. Buy a product
  3. Restore purchases.

References:

  1. Guides and Sample Code. In-App Purchase Programming Guide
  2. API Reference. StoreKit.
  3. WWDC 2016. Session 702
  4. Technical Note TN2387. In-App Purchase Best Practices

How to use

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.