Swift3 - 恢复应用内购买交易时获取交易日期

时间:2017-09-20 10:39:47

标签: ios swift transactions in-app-purchase

我正在尝试恢复应用内购买(非续订订阅),以防用户在新手机上安装应用或在手机上重新安装应用。我想使用方法

获取应用内购买的原始购买日期
 func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {

        for transaction in queue.transactions{
            let t : SKPaymentTransaction = transaction
            let prodID = t.payment.productIdentifier as String
            print("transaction finished",prodID)
            if(prodID == "com.netvariant.FamousFaces.FullAccess"){
                print(">>>>>>>>>>>>>", t.original?.transactionDate)
                print(">>>>>>>>>>>>>", t.transactionDate)
                defaults.set("Y", forKey: "adPurchase")
                NotificationCenter.default.post(name: Notification.Name(rawValue: "removeAds"), object: nil)
                print("working")
            }

        }
    }

问题是在两种情况下返回nil的交易日期。我在代码中错过了什么?这是处理恢复购买的最佳方式吗?

我想知道用户的订阅期限(1个月)是否结束的日期。恢复是成功的,但正如我提到的日期为零。

谢谢。

1 个答案:

答案 0 :(得分:1)

您可以使用SwiftyStoreKit进行验证并执行InApp购买。以下代码用于验证InApp购买是否已过期。对于用户此代码,您项目中的SwiftyStoreKit

let appleValidator = AppleReceiptValidator(service: .production)
SwiftyStoreKit.verifyReceipt(using: appleValidator, password: "Enter your inapp secret key") { result in
    switch result {
    case .success(let receipt):
        // Verify the purchase of a Subscription
        let purchaseResult = SwiftyStoreKit.verifySubscription(
            type: .autoRenewable, // or .nonRenewing (see below)
            productId: "Enter your inapp product key",
            inReceipt: receipt)

        switch purchaseResult {
        case .purchased(let expiryDate, _):

            print("Product is valid until \(expiryDate)")
        case .expired(let expiryDate, _):

            print("Product is expired since \(expiryDate)")
        case .notPurchased:
            print("The user has never purchased this product")
        }

    case .error(let error):
        print("Receipt verification failed: \(error)")
    }
}