在iOS中的应用程序购买中如果按取消按钮则没有任何反应

时间:2016-11-28 14:26:45

标签: ios swift in-app-purchase

我在应用购买应用程序中做得很简单。在产品加载时我只是恢复以前的所有购买,所以当我开始加载产品时我开始加载指示器,但在某些设备中我已经使用了inapp id,因此每次我在该设备中加载产品时会出现某种类型的警报,如果我按下取消然后什么也没发生,我的加载指示器无限。

我也有截图。

enter image description here

extension IAPHelper: SKPaymentTransactionObserver {

    public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {

        for transaction in transactions {
            if productIdentifiers.contains(transaction.payment.productIdentifier) {
                switch (transaction.transactionState) {

                case .purchased:
                    complete(transaction: transaction)
                    break
                case .failed:
                    fail(transaction: transaction)
                    break
                case .restored:
                    restore(transaction: transaction)
                    break
                case .deferred:
                    print("denied")
                    break
                case .purchasing:
                    break
                }
            }
        }
        if productsRestoreCompletionHandler != nil
        {
            productsRestoreCompletionHandler = nil
        }

    }


    private func complete(transaction: SKPaymentTransaction) {
        print("complete...")
//        deliverPurchaseNotificationFor(identifier: transaction.payment.productIdentifier)
        SKPaymentQueue.default().finishTransaction(transaction)

        productsPurchaseCompletionHandler?(true, transaction)
        if productsPurchaseCompletionHandler != nil
        {
            productsPurchaseCompletionHandler = nil
        }
    }

    private func restore(transaction: SKPaymentTransaction) {
        guard let productIdentifier = transaction.original?.payment.productIdentifier else { return }

        print("restore... \(productIdentifier)")
//        deliverPurchaseNotificationFor(identifier: productIdentifier)
        SKPaymentQueue.default().finishTransaction(transaction)
        productsRestoreCompletionHandler?(true, transaction)
    }

    private func fail(transaction: SKPaymentTransaction) {
        print("fail...")
        if let transactionError = transaction.error as? NSError {
            if transactionError.code != SKError.paymentCancelled.rawValue {
                print("Transaction Error: \(transaction.error?.localizedDescription)")
            }
        }

        SKPaymentQueue.default().finishTransaction(transaction)
        if productsRestoreCompletionHandler != nil
        {
            productsRestoreCompletionHandler?(false, transaction)
            productsRestoreCompletionHandler = nil
        }
        if productsPurchaseCompletionHandler != nil
        {
            productsPurchaseCompletionHandler?(false, transaction)
            productsPurchaseCompletionHandler = nil
        }
    }

    private func deliverPurchaseNotificationFor(identifier: String?) {
        guard let identifier = identifier else { return }

        purchasedProductIdentifiers.insert(identifier)
//        UserDefaults.standard.set(true, forKey: identifier)
//        UserDefaults.standard.synchronize()
//        NotificationCenter.default.post(name: NSNotification.Name(rawValue: IAPHelper.IAPHelperPurchaseNotification), object: identifier)
    }
}
  

产品加载时

//MARK:
//MARK: load the product


 func getProduct()
    {

        let userData = getUserDataFromFile()
        obj = IAPHelper(productIds: NetFishProducts.productIdentifiers)

        if checkInternetConnection()
        {
            SINGLETON.startLoadingActivity(self.view)
            obj.requestProducts { (isSuccess, arrProduct) in

                if isSuccess == true
                {
                    self.setProduct = Set<SKProduct>(arrProduct! as [SKProduct])
                    self.btnSignup.isUserInteractionEnabled = true
                    for p in arrProduct! {
                        print("Found product: \(p.productIdentifier) \(p.localizedTitle) \(p.price.floatValue)")
                        if p.productIdentifier == NetFishProducts.monthlyPlan
                        {
                            self.setupLblMonth(withMothPrice: "\(p.price.floatValue)")
                        }
                        else
                        {
                            self.setupLblYear(withYearPrice: "\(p.price.floatValue)")
                        }
                    }
                    self.obj.requestRestoreProducts(completionHandler: { (isSuccess, transation) in
                        SINGLETON.stopLoadingActivity(self.view)
                        if isSuccess == true
                        {
                            let orignalTI = transation?.original?.transactionIdentifier
                            let userEmail = transation?.payment.applicationUsername
                            print("orignalTI:\(orignalTI)")
                            print("username:\(userEmail)")
                            if userEmail != nil && userData?.userEmail != userEmail
                            {
                                self.btnSignup.isUserInteractionEnabled = false
                                self.showAlertToUser("Information", MsgDesc: "You are login with different appstore account so login with correct appstore account")
                                return
                            }
                        }
                        else
                        {
                            self.btnSignup.isUserInteractionEnabled = false
                        }

                        },withUserName: (userData?.userEmail)!)

                }
                else
                {
                    SINGLETON.stopLoadingActivity(self.view)
                    self.btnSignup.isUserInteractionEnabled = false
                }
            }
        }
    }

0 个答案:

没有答案