ios - How do I add multiple in app purchases in Swift Spritekit? -
i have in app purchase unlock level 2 , works how add in app purchase unlock level 3? have make new functions , add code level3? im not sure how this. thanks!
func unlocklevel() { if skpaymentqueue.canmakepayments() { var productid:nsset = nsset(object: "unlockleveltwo") var productsrequest:skproductsrequest = skproductsrequest(productidentifiers: productid set<nsobject>) productsrequest.delegate = self productsrequest.start() println("fetching products...") } else { println("can't make purchases") } } func buyproduct(product: skproduct) { println("sending payment request 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!) { 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 2 has not been purchased on 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 apple"); 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; } } } }
Comments
Post a Comment