I made a new sandbox test user to test the restore purchases button but when I press it and put my password or when I press cancel the levels still unlock. I don't get why this is happening. Can someone take a look at my code and tell me what Im doing wrong? Thanks!
func RestorePurchases() {
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
println("Can't make purchases")
}
func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!) {
println("transactions restored")
var purchasedItemIDS = []
for transaction in queue.transactions {
var t: SKPaymentTransaction = transaction as! SKPaymentTransaction
let prodID = t.payment.productIdentifier as String
switch prodID {
case "unlockLevelTwo":
println("restoreleveltwo")
NSUserDefaults().setBool(true, forKey: "Leavel2")
unlockLevelTwoPlease()
case "unlockLevelThree":
println("restorelevelthree")
NSUserDefaults().setBool(true, forKey: "Leavel3")
unlockLevelThreePlease()
default:
println("IAP not setup")
let alert = UIAlertView()
alert.title = "Oops"
alert.message = "There are no purchases to restore, please buy one"
alert.addButtonWithTitle("Cancel")
alert.show()
}
}
}
//EDITED MORE CODE....
func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
println("Received Payment Transaction Response from Apple");
for transaction:AnyObject in transactions {
var trans = transaction as! SKPaymentTransaction
println(trans.error)
switch trans.transactionState {
case .Purchased:
let prodID = p.productIdentifier as String
switch prodID {
case "unlockLevelTwo":
println("unlocksleveltwo")
NSUserDefaults().setBool(true, forKey: "Leavel2")
unlockLevelTwoPlease()
case "unlockLevelThree":
println("unlocklevelthree")
NSUserDefaults().setBool(true, forKey: "Leavel3")
unlockLevelThreePlease()
default:
println("")
}
queue.finishTransaction(trans)
break;
case .Failed:
println("Purchased Failed");
queue.finishTransaction(trans)
break;
case .Restored:
let prodID = p.productIdentifier as String
switch prodID {
case "unlockLevelTwo":
println("unlocksleveltwo")
NSUserDefaults().setBool(true, forKey: "Leavel2")
unlockLevelTwoPlease()
case "unlockLevelThree":
println("unlocklevelthree")
NSUserDefaults().setBool(true, forKey: "Leavel3")
unlockLevelThreePlease()
default:
println("")
}
default:
println("default")
break;
}
}
}
I think you're in the wrong method.
From Apple documentation,
paymentQueueRestoreCompletedTransactionsFinished
tells the observer that the payment queue has finished sending restore transactions. This method is called after all restorable transactions have been processed by the payment queue. Your application is nos required to do anything in this method.I suppose you've implemented
paymentQueue:updatedTransactions
. You should examine the transactionStateSKPaymentTransactionStateRestored
and unlock your levels there.