如何在Swift Spritekit中添加多个应用内购买?

时间:2015-06-04 14:00:08

标签: ios swift in-app-purchase

我在应用程序购买中解锁2级并且它可以工作,但如何在应用程序购买中添加另一个解锁3级?我是否必须创建新功能并添加level3的代码?我不知道该怎么做。谢谢!

    func unlockLevel() {
    if SKPaymentQueue.canMakePayments() {
        var productID:NSSet = NSSet(object: "unlockLevelTwo")
        var productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as Set<NSObject>)
        productsRequest.delegate = self
        productsRequest.start()
        println("Fetching products...")
    }
    else {
        println("Can't make purchases")
    }
}

func buyProduct(product: SKProduct) {
    println("Sending the Payment Request to Apple");
    var payment = SKPayment(product: product)
    SKPaymentQueue.defaultQueue().addPayment(payment);
}

func productsRequest(request: SKProductsRequest!, didReceiveResponse response: SKProductsResponse!) {
    var count : Int = response.products.count
    if (count>0) {
        var validProducts = response.products
        var validProduct: SKProduct = response.products[0] as! SKProduct
        if (validProduct.productIdentifier == "unlockLevelTwo") {
            println(validProduct.localizedTitle)
            println(validProduct.localizedDescription)
            println(validProduct.price)
            buyProduct(validProduct);
        }
        else {
            println(validProduct.productIdentifier)
        }
    }
    else {
        println("nothing")
    }
}

func request(request: SKRequest!, didFailWithError error: NSError!) {
    println("Error fetching product info")
}

func RestorePurchases() {
    SKPaymentQueue.defaultQueue().restoreCompletedTransactions()


}

func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!) {

    for item in queue.transactions {
        if let transaction = item as? SKPaymentTransaction {
            if transaction.transactionState == .Restored {
                unlockLevel()
                SKPaymentQueue.defaultQueue().finishTransaction(transaction)
                break
            }
        }
    }
    if queue.transactions.count == 0 {
        println("not restored")
        displayRestoreAlert()
    }
}

func displayRestoreAlert() {

    let alertController = UIAlertController(title: "Level Two has not been purchased on this account.", message: nil, preferredStyle: .Alert)
    let OKAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
    alertController.addAction(OKAction)
    self.viewController.presentViewController(alertController, animated: true, completion: nil)

}



func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
    println("Received Payment Transaction Response from Apple");

    for transaction:AnyObject in transactions {
        if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
            switch trans.transactionState {
            case .Purchased:
                println("Product Purchased");
                SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                NSUserDefaults().setBool(true, forKey: "Leavel2")
                unlockLevelTwo()
                break;
            case .Failed:
                println("Purchased Failed");
                SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                break;
            case .Restored:
                println("Already Purchased");
                SKPaymentQueue.defaultQueue().restoreCompletedTransactions()

            default:
                break;
            }
        }
    }
}

0 个答案:

没有答案