Как исправить: SKProduct возвращается ноль - PullRequest
0 голосов
/ 23 июня 2019

IAP SKProduct имеет постоянное значение nil, и я не уверен, почему!

extension GameViewController {

    //MARK: - Call purchase

    //MARK: Product

    func requestProductData() {
        if(SKPaymentQueue.canMakePayments()) {

            let productIds: Set<String> = [SettingsGlobal.InApp.removingAds]
            print("productIds \(productIds)")
            let productRequest = SKProductsRequest(productIdentifiers: productIds)
            productRequest.delegate = self
            productRequest.start()
        }
        else { print("Cannot make payment")}
    }



    @objc func buyRemoveAds() {
        requestProductData()
        print("test \(String(describing: removingAdsIDs)), canMakePayments: \(SKPaymentQueue.canMakePayments())")
        tryToBuyProduct(removingAdsIDs)
    }

    func removeAdsPurchase() {
        UserDefaults.standard.set(true, forKey: "adRemovedByPurchase")
    }

    //MARK: Store

    func tryToBuyProduct(_ product: SKProduct?) {
        if(SKPaymentQueue.canMakePayments()) {
            if product != nil {
                let payment = SKPayment(product: product!)
                SKPaymentQueue.default().add(payment)
            }
        }
        else {
            let alert = UIAlertController(title: "In-App Purchases Not Enabled", message: "Please enable In App Purchase in Settings", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "Settings", style: UIAlertActionStyle.default, handler: { alertAction in
                alert.dismiss(animated: true, completion: nil)

                let url: NSURL? = NSURL(string: UIApplicationOpenSettingsURLString)
                if url != nil { UIApplication.shared.openURL(url! as URL)}
            }))
            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: { alertAction in
                alert.dismiss(animated: true, completion: nil)
            }))
            self.present(alert, animated: true, completion: nil)
        }
    }

    func deliverProduct(transaction:SKPaymentTransaction) {
        if transaction.payment.productIdentifier == SettingsGlobal.InApp.removingAds { removeAdsPurchase()}

        NotificationCenter.default.post(name: Notification.Name(rawValue: "purchaseIsCompleate_StoreScene"), object: nil)
    }

    //Restore purchases

    @objc func restorePurchases () {
        if SKPaymentQueue.canMakePayments() { SKPaymentQueue.default().restoreCompletedTransactions()}
    }

    //MARK: - Process

    public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
        for transaction in transactions {
            switch transaction.transactionState {
            case .purchased:
                print("Purchase compleate")
                deliverProduct(transaction: transaction)
                break
            case .failed:
                print("Purchase failed")
                print(transactions)
                break
            case .purchasing:
                print("Purchase purchasing")
                break
            case .deferred:
                print("Purchase deferred")
                break
            case .restored:
                deliverProduct(transaction: transaction)
                break;
            }
        }
    }

    public func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
        let products = response.products

        if products.count != 0 {
            for product in products {
                print(product.productIdentifier)
                switch product.productIdentifier {
                case SettingsGlobal.InApp.removingAds:
                    removingAdsIDs = product
                default:
                    print("")
                }
            }
        }
    }
}

Функция buyRemoveAds возвращает строку печати, показывая, что SKProduct равен нулю.test nil, canMakePayments: true

Это мой класс Game View Controller, представление, которое загрузилось, запускает [requestProductData]

  func requestProductData() {
        if(SKPaymentQueue.canMakePayments()) {

            let productIds: Set<String> = [SettingsGlobal.InApp.removingAds]
            print("productIds \(productIds)")
            let productRequest = SKProductsRequest(productIdentifiers: productIds)
            productRequest.delegate = self
            productRequest.start()
        }
        else { print("Cannot make payment")}
    }
class GameViewController: UIViewController, GKGameCenterControllerDelegate, GADInterstitialDelegate, GADBannerViewDelegate, SKProductsRequestDelegate, SKPaymentTransactionObserver, UITableViewDelegate, UITableViewDataSource {

    //MARK: - Values

    var intersitialRecievedAd = false
    var interstitial = GADInterstitial(adUnitID: SettingsGlobal.adIdInterstitial)
    var banner = GADBannerView(adSize: kGADAdSizeBanner)

    var tableViewGameScore = UITableView()

    var removingAdsIDs: SKProduct?


    //MARK: - Work with view

    override func viewDidLoad() {
        super.viewDidLoad()
        requestProductData()
        SKPaymentQueue.default().add(self)

Что мне здесь не хватает, это потеря переменных данныхв одной форме или в форме?

Каков наилучший способ отладки этого?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...