How do I add multiple in app purchases in Swift Spritekit?

696 views Asked by At

I have this in app purchase to unlock level 2 and it works but how do I add another in app purchase to unlock level 3? Do I have to make new functions and add the code for level3? Im not sure how to do this. Thanks!

    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

There are 0 answers