如何修复:SKProduct返回nil

时间:2019-06-23 00:55:40

标签: ios swift in-app-purchase

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为nil。 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)

我在这里缺少什么,可变数据是否以一种方式丢失?

  

什么是最好的调试方法?

0 个答案:

没有答案