首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查用户是否已经在SwiftyStoreKit中购买了产品

检查用户是否已经在SwiftyStoreKit中购买了产品
EN

Stack Overflow用户
提问于 2019-07-18 11:50:10
回答 1查看 1.9K关注 0票数 2

我实现了应用程序内的购买,我使用的是SwiftyStoreKit。一切正常,但要检查用户是否已经购买了该产品。我想当用户进入不合适的页面显示一个按钮。如果它买了,然后显示“打开”,如果不是“价格”。我不太明白我怎么能做到这一点。

viewDidLoad():

代码语言:javascript
复制
let inAppPurchaseId = "iD"
    let sharedSecret = "shared secret"

override func viewDidLoad() {
        super.viewDidLoad()

        SwiftyStoreKit.retrieveProductsInfo([inAppPurchaseId]) { result in
            if let product = result.retrievedProducts.first {
                let priceString = product.localizedPrice!
                print("Product: \(product.localizedDescription), price: \(priceString)")
                self.buyBtn.setTitle("Buy guides for "+"\(priceString)", for: .normal)


                self.verifyPurchase(with: self.inAppPurchaseId, sharedSecret: self.sharedSecret)

            }
            else if let invalidProductId = result.invalidProductIDs.first {
                print("Invalid product identifier: \(invalidProductId)")
            }
            else {
                print("Error: \(String(describing: result.error))")
            }
        }

    }

这是收据验证和你的purchaseProduct函数。

代码语言:javascript
复制
func purchaseProduct(with id: String) {
        SwiftyStoreKit.retrieveProductsInfo([id]) { result in
            if let product = result.retrievedProducts.first {
                SwiftyStoreKit.purchaseProduct(product, quantity: 1, atomically: true) { result in
                    switch result {
                    case .success(let product):
                        // fetch content from your server, then:
                        if product.needsFinishTransaction {
                            SwiftyStoreKit.finishTransaction(product.transaction)
                        }
                        self.buyBtn.setTitle("Open", for: .normal)
                        print("Purchase Success: \(product.productId)")
                    case .error(let error):
                        switch error.code {
                        case .unknown: print("Unknown error. Please contact support")
                        case .clientInvalid: print("Not allowed to make the payment")
                        case .paymentCancelled: break
                        case .paymentInvalid: print("The purchase identifier was invalid")
                        case .paymentNotAllowed: print("The device is not allowed to make the payment")
                        case .storeProductNotAvailable: print("The product is not available in the current storefront")
                        case .cloudServicePermissionDenied: print("Access to cloud service information is not allowed")
                        case .cloudServiceNetworkConnectionFailed: print("Could not connect to the network")
                        case .cloudServiceRevoked: print("User has revoked permission to use this cloud service")
                        default: print((error as NSError).localizedDescription)
                        }
                    }
                }
            }
        }
    }

    func verifyPurchase(with id: String, sharedSecret: String) {
        let appleValidator = AppleReceiptValidator(service: .production, sharedSecret: sharedSecret)
        SwiftyStoreKit.verifyReceipt(using: appleValidator) { result in
            switch result {
            case .success(let receipt):
                let productId = id
                // Verify the purchase of Consumable or NonConsumable
                let purchaseResult = SwiftyStoreKit.verifyPurchase(
                    productId: productId,
                    inReceipt: receipt)

                switch purchaseResult {
                case .purchased(let receiptItem):
                    print("\(productId) is purchased: \(receiptItem)")
                case .notPurchased:
                    self.purchaseProduct(with: self.inAppPurchaseId)
                    print("The user has never purchased \(productId)")
                }
            case .error(let error):
                print("Receipt verification failed: \(error)")
            }
        }
    }

我想要检查什么时候视图加载按钮应该有什么标题,以及产品是否已经购买。

EN

回答 1

Stack Overflow用户

发布于 2019-12-06 07:03:57

您需要保存该产品是否已购买。示例UserDefaults (未加密),但可以使用KeychainWrapper。首先尝试使用UserDefaults.standard.bool而不是KeychainWrapper。

对于价格/打开按钮,使用fullPurchase并更改标题和逻辑。

你有两个案子:

  1. 案例.purchased(let receiptItem):
  2. 案例.notPurchased: 变量fullPurchase: Bool?{ get { fullPurchasedFromKeychain = KeychainWrapper.standard.bool(forKey: FullAccessOlder) {返回fullPurchasedFromKeychain }返回假} set { KeychainWrapper.standard.set(newValue!,forKey: FullAccessOlder) }} func verifyPurchase(带id: String,sharedSecret: String) { let appleValidator =AppleReceiptValidator(服务:.production,sharedSecret: sharedSecret) SwiftyStoreKit.verifyReceipt(使用: appleValidator) {导致切换结果{ case .success(让收据):let productId = id /验证购买消耗品或NonConsumable let purchaseResult = SwiftyStoreKit.verifyPurchase( productId: productId,inReceipt:接收) purchaseResult { case .purchased(let receiptItem):print("(productId)被购买:(receiptItem)") self.fullPurchase = true case .notPurchased: self.purchaseProduct(with: self.inAppPurchaseId)打印(“用户从未购买(productId)") self.fullPurchase = false } case .error(让错误):print(”收据验证失败:(错误)“)}

有关消费品的信息将在收到付款后添加到收据中,并保留在收据中,直到您完成交易。在您完成事务后,下次更新收据时将删除此信息-例如,下次用户进行购买时。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57093797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档