An ios package for in app purchase integration.
- Xcode -> File -> Swift Packages -> Add Package Dependencies
- put
https://git.ebpearls.com/ios-packages/iap.git
and follow the prompts.
- Determine product identifiers used in your app and create enum value for them using
IAPProductIdentifiable
protocol.enum IAPIdentifiers: IAPProductIdentifiable, CaseIterable { case yearly case monthly var term: PaymentTerm { switch self { case .yearly: return .yearly case .monthly: return .monthly } } var identifier: String { switch self { case .yearly: return "YEARLY_SUBSCRIPTION_IDENTIFIER" case .monthly: return "MONTHLY_SUBSCRIPTION_IDENTIFIER" } } }
- Listen for the product info.
// listen for products fetch result IAP.shared.products.receive(on: RunLoop.main).sink { [weak self] products in guard let self = self else { return } // STORE FOR YOUR USAGE WITHIN APP, These are the instance of IAPProduct }.store(in: &bag)
- Pass the identifiers to IAP class so that their details are retrieved from appstore and the result are listened from step 2.
IAP.shared.setProducts(availableProducts: Constant.IAPIdentifiers.allCases)
- For purchasing
IAP.shared.purchase(product: /*IAPProduct*/)
- Listen for the purchasing states
// states when purchase is in progress IAP.shared.productPurchaseState.receive(on: RunLoop.main).sink { [weak self] state in guard let self = self else { return } switch state { case .failed(let error): /// handle errors properly case .purchased(let inAppPurchase): /// The purchase info is received here if completed default: break } }.store(in: &viewModel.bag)
- Validating and other logic are handled by server. We can fetch latest receipt to be able to restore in server using following method from IAP.
public func fetchLatestReceipt(force: Bool = false, completion: @escaping (_ receiptData: Data?, _ error: ReceiptError?) -> Void)