如何在 IAP Swift 中获得正确的原始购买日期和过期日期

How to get the correct original purchase and expires date in IAP Swift

我目前正在测试沙盒中的应用内购买,以查看用户订阅当前是否有效或已过期。我刚订阅了一个月,收据上说订阅马上就过期了。 originalPurchaseDate 和 ExpiryDate 看起来都不对。我如何确定 json 中返回的日期是正确的。我将 expiryDate 与原始购买日期进行比较,以确定订阅是否仍然有效。

func checkIfSubscriptionIsActive() {
    IAPManager.shared.validatePurchaseReceipt { (pReceipt) in
        self.receipt = pReceipt
        let expiryDate = self.receipt.last.map { [=11=].expiresDate}!.skTodate
        let originalPurchaseDate = self.receipt.last.map { [=11=].originalPurhaseDate}!.skTodate
        print("Latest Receipt: \(self.receipt.last)")
        if expiryDate?.compare(originalPurchaseDate!) == .orderedDescending {
            print("Subscription is expired")
        } else {
            print("Subscription is still active")
        }
    }
}

这是我的日志

Latest Receipt: Optional(Vinylly.IAPReceiptInfo(quantity: "1", tansactionID: "1000000500500768", productID: "com.myApp.skLLC.autoRenewableSubscription", originalPurhaseDate: "2019-01-17 11:50:53 Etc/GMT", isTrialPeriod: "false", webOrderLineItem: "1000000042609588", expiresDate: "2019-02-06 15:55:21 Etc/GMT"))

根据 Apple 文档,当您在沙盒环境中测试时,一个月的订阅将在 5 分钟后到期。如果您想 write/test 围绕此编写一些代码,请尝试在 5 分钟内完成。