快速购买应用程序后如何解锁UITabbar

时间:2018-08-06 06:45:13

标签: swift in-app-purchase uitabbarcontroller uitabbar

我是刚问问题的人。

在创建该应用程序的功能时需要我的帮助,该功能是在购买时解锁tabbar

我使用此代码锁定了tabbar中的override func viewDidLoad()

如果有人能为我解决这个问题,我将不胜感激!我整天都在研究,没有任何答案。谢谢!

   if  let arrayOfTabBarItems = tabBarController?.tabBar.items as AnyObject as? NSArray,let tabBarItem1 = arrayOfTabBarItems[0] as? UITabBarItem {
            tabBarItem1.isEnabled = true
    }

    if  let arrayOfTabBarItems = tabBarController?.tabBar.items as AnyObject as? NSArray,let tabBarItem2 = arrayOfTabBarItems[1] as? UITabBarItem {
        if nonConsumablePurchaseMade == true {
           tabBarItem2.isEnabled = true
            print("done")
        } else
        {
             tabBarItem2.isEnabled = false
            print("failed")

        }

    }
    if  let arrayOfTabBarItems = tabBarController?.tabBar.items as AnyObject as? NSArray,let tabBarItem3 = arrayOfTabBarItems[2] as? UITabBarItem {
        tabBarItem3.isEnabled = false
    }
    if  let arrayOfTabBarItems = tabBarController?.tabBar.items as AnyObject as? NSArray,let tabBarItem4 = arrayOfTabBarItems[3] as? UITabBarItem {
        tabBarItem4.isEnabled = false
    }

,这可能会有所帮助。它们是购买的功能。

 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)

                    }

                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")
                    }
                }
            }
        }
    }

}

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 = "myID"
            // Verify the purchase of Consumable or NonConsumable
            let purchaseResult = SwiftyStoreKit.verifyPurchase(
                productId: id,
                inReceipt: receipt)

            switch purchaseResult {
            case .purchased(let receiptItem):
                print("\(productId) is purchased: \(receiptItem)")
                if self.product_ID == self.productId {

                    // Save your purchase locally (needed only for Non-Consumable IAP)
                    self.nonConsumablePurchaseMade = true
                    UserDefaults.standard.set(self.nonConsumablePurchaseMade, forKey: "nonConsumablePurchaseMade")

                }
            case .notPurchased:
                print("The user has never purchased \(productId)")
            }
        case .error(let error):
            print("Receipt verification failed: \(error)")
        }
    }
}


@objc func returnTextView(gesture: UIGestureRecognizer) {
    guard activeField != nil else {
        return
    }

    activeField?.resignFirstResponder()
    activeField = nil
}

0 个答案:

没有答案