在我的应用程序中,我有 3 个消耗品应用内购买。我已经在沙盒环境中对它们进行了测试,它们按预期工作。我使用该SwiftyStoreKit
框架进行应用内购买。
以下代码用于执行购买。
func purchase(_ purchase: String, quantity: Int = 1, completion: @escaping ((_ result: PurchaseDetails?)->Void)) {
ProgressIndicatorManager.shared.show()
SwiftyStoreKit.purchaseProduct(purchase) { (result) in
ProgressIndicatorManager.shared.dismiss()
if case .success(let purchase) = result {
// Deliver content from server, then:
if purchase.needsFinishTransaction {
SwiftyStoreKit.finishTransaction(purchase.transaction)
}
completion(purchase)
} else {
completion(nil)
}
if let alert = self.alertForPurchaseResult(result) {
self.showAlert(alert)
}
}
}
购买完成后,我会验证收据。
/// This function is use for verify receipt
/// - Parameters:
/// - purchase: purchase identifier
func verifyPurchase(_ purchase: String, completion: @escaping (VerifyPurchaseResult) -> Void) {
ProgressIndicatorManager.shared.show()
verifyReceipt { result in
ProgressIndicatorManager.shared.dismiss()
switch result {
case .success(let receipt):
let purchaseResult = SwiftyStoreKit.verifyPurchase(
productId: purchase,
inReceipt: receipt)
self.showAlert(self.alertForVerifyPurchase(purchaseResult, productId: purchase))
completion(purchaseResult)
case .error:
self.showAlert(self.alertForVerifyReceipt(result))
}
}
}