在应用程序中购买SKScene swift

时间:2017-12-07 11:52:27

标签: swift sprite-kit

在我的应用程序中,我有以下代码来处理IAP:

var list = [SKProduct]()
var p = SKProduct()

func buyProduct() {

    print("buy" + p.productIdentifier)

    let pay = SKPayment(product: p)
    SKPaymentQueue.default().add(self)
    SKPaymentQueue.default().add(pay as SKPayment)
}

func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {

    print("prudcutRequest")

    let myProduct = response.products

    for product in myProduct {

        print(product.productIdentifier)
        print(product.price)
        print(product.localizedTitle)
        print(product.localizedDescription)

        list.append(product)
    }
}

func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {

    print("transaction restored")

    for transaction in queue.transactions {

        let t:SKPaymentTransaction = transaction

        let prodID = t.payment.productIdentifier as String

        switch prodID {

        case "stars100": GameScene.numberOfStars += 100

        default: print("IAP not setup")
        }
    }

    let alert = UIAlertView(title: "Thank you", message: "Your purchase is completed", delegate: nil, cancelButtonTitle: "OK")
    alert.show()
}

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

    print("add Payment")

    for transaction:AnyObject in transactions {

        let trans = transaction as! SKPaymentTransaction

        switch trans.transactionState {

        case .purchased, .restored: print("buy" + p.productIdentifier)

            let prodID = p.productIdentifier as String

        switch prodID {

        case "stars100": GameScene.numberOfStars += 100

            let alert = UIAlertView(title: "Thank you 1", message: "", delegate: nil, cancelButtonTitle: "OK")
            alert.show()

        default: print("IAP not setup")
            }

            queue.finishTransaction(trans)
        break;
        case .failed: print("buy error")
            queue.finishTransaction(trans)
        break;
        default: print("default")
        break;
        }
    }
}

func finishTransaction(trans: SKPaymentTransaction) {print("finish trans")}

func paymentQueue(_ queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) {print("remove trans")}

override func didMove(to view: SKView) {

    if (SKPaymentQueue.canMakePayments()) {

        print("IAP is enabled, loading")

        let productID:NSSet = NSSet(object: "stars100")
        let request:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as Set<NSObject> as! Set<String>)
        request.delegate = self
        request.start()
    } else {print("please enable IAPs")}

然后我按下此按钮购买产品:

if getButton1.contains(location) {

            for product in list {

                let prodID = product.productIdentifier

                if (prodID == "stars100") {

                    p = product

                    buyProduct()

                    break;
                }
            }
        }

我的问题是,当我触摸按钮时,我在控制台中只看到这些消息: IAP已启用,正在加载 prudcutRequest

在应用程序中没有出现alertView。

如何解决此问题并完成IAP?

0 个答案:

没有答案