SwiftyStoreKit如何检查用户是否购买了产品?

时间:2017-12-29 14:26:20

标签: ios swift swiftystorekit

如何使用SwiftyStoreKit检查用户是否购买了产品ID为com.X.X的产品?我想使用SwiftyStoreKit代码来检查用户是否已经使用inapp购买系统购买了产品?我想这样做是为了确定我是否应该向他们展示一个功能。如何使用Apple的inapp购买系统检查用户是否已购买产品?使用swift

1 个答案:

答案 0 :(得分:0)

func checkIfPurchaed () {
    let appleValidator = AppleReceiptValidator(service: .production, sharedSecret: "your-shared-secret")
    SwiftyStoreKit.verifyReceipt(using: appleValidator) { result in
        switch result {
        case .success(let receipt):
            let productId = "com.app.name.productID"
            // Verify the purchase of a Subscription
            let purchaseResult = SwiftyStoreKit.verifySubscription(
                ofType: .renewing //or .nonRenewing
                productId: productId,
                inReceipt: receipt)

            switch purchaseResult {
            case .purchased(let expiryDate, let items):
                print("\(productId) is valid until \(expiryDate)\n\(items)\n")
            case .expired(let expiryDate, let items):
                print("\(productId) is expired since \(expiryDate)\n\(items)\n")
            case .notPurchased:
                print("The user has never purchased \(productId)")
            }

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

}