使用第三方库将应用购买价值存储在钥匙串中

时间:2018-10-19 15:42:14

标签: ios swift in-app-purchase keychain

我有一个要整合到应用程序购买中的应用程序,并且我使用的SwiftyStoreKit效果很好。整个应用内购买都可以正常工作,但是我现在面临的问题是我有一些禁用的按钮,一旦用户进行购买并且购买成功,我希望启用这些按钮。使用Userdefaults可能是一个简单的方法,但是它并不安全,因此我觉得使用keychain更好,因此我决定将带有此第三方库KeychainAccess的钥匙串合并到我的具有第三方库的应用程序中而我的代码如下所示

func setNonConsumablePurchase(_ status: Bool, identifier: String?) {

        let keychain = Keychain(service: "com.companyname.appname.iapService")
        guard let identifier = identifier else {return}
        UserDefaults.standard.set(status, forKey: "nonConsumablePurchaseWasMade")
        do {
            try keychain.set("purchased", key: IAPsConfig.appBundle + "." + identifier)
            }
            catch let error {
                print("setting keychain to purchased failed")
                print(error)
            }
    }

    func isProductPurchased(identifier: IAPsConfig.RegisteredPurchase, completion: @escaping CompletionHandler) {

        let keychain = Keychain(service: "com.companyname.appname.iapService")
        let value = IAPsConfig.appBundle + "." + identifier.rawValue


        // if there is value correspond to the productIdentifier key in the keychain
        if (try? keychain.get(identifier.rawValue)) == value{

            completion(true)

        } else {
            // the product has not been purchased previously, do nothing
            print("Not purchased: \(identifier)")
        }

    }



    func purchase(viewController: UIViewController, purchase: IAPsConfig.RegisteredPurchase, sharedSecret: String, type: IAPsConfig.PurchaseType, atomically: Bool, validDuration: TimeInterval? = nil) -> Void {


        NetworkActivityIndicatorManager.networkOperationStarted()
        if type == .simple {
            SwiftyStoreKit.retrieveProductsInfo([IAPsConfig.appBundle + "." + purchase.rawValue]) { result in
                if let product = result.retrievedProducts.first {
                    SwiftyStoreKit.purchaseProduct(product, quantity: 1, atomically: atomically) { result in

                        NetworkActivityIndicatorManager.networkOperationFinished()

                        if case .success(let purchased) = result {
                            let downloads = purchased.transaction.downloads
                            if !downloads.isEmpty {
                                SwiftyStoreKit.start(downloads)
                            }
                            self.setNonConsumablePurchase(true, identifier: purchase.rawValue)
                            // Deliver content from server, then:
                            if purchased.needsFinishTransaction {
                                SwiftyStoreKit.finishTransaction(purchased.transaction)
                            }
                        }
                        if let alert = IAPAlerts.instance.alertForPurchaseResult(result) {
                            self.showAlert(alert, viewController: viewController)
                        }
                    }
                }
            }
        } else {

            SwiftyStoreKit.purchaseProduct(IAPsConfig.appBundle + "." + purchase.rawValue, atomically: atomically) { result in
                NetworkActivityIndicatorManager.networkOperationFinished()

                if case .success(let purchased) = result {
                    let downloads = purchased.transaction.downloads
                    if !downloads.isEmpty {
                        SwiftyStoreKit.start(downloads)
                    }
                    self.setNonConsumablePurchase(true, identifier: purchase.rawValue)
                    // Deliver content from server, then:
                    if purchased.needsFinishTransaction {
                        SwiftyStoreKit.finishTransaction(purchased.transaction)
                    }
                }

                if let alert = IAPAlerts.instance.alertForPurchaseResult(result) {
                    self.showAlert(alert, viewController: viewController)
                }

                guard let validDuration = validDuration else {return}
                self.verifyPurchase(viewController: viewController, purchase, validDuration: validDuration)
            }
        }

    }

我的viewcontroller

 @IBAction func buyAutoRenew1(_ sender: Any) {
        IAPServices.instance.purchase(viewController: self, purchase: .Subscription1, sharedSecret: IAPsConfig.sharedSecret, type: .autoRenew, atomically: true, validDuration: TimeInterval(60))
    }

 override func viewDidLoad() {
        super.viewDidLoad()
        IAPServices.instance.isProductPurchased(identifier: .Subscription1) { (success) in

            if success {
                print("BOUGHT ALREADY")
            } else {
                print("Not BOUGHT AT ALL")
            }
        }
    }

再次购买并加载Viewcontroller后,我仍然得到

Not purchased: Subscription1

我不知道自己在做错什么,如果有人能指出正确的方向,我会很高兴

0 个答案:

没有答案