Last active
January 5, 2016 14:21
-
-
Save mhomol/9937931206584bb94b3d to your computer and use it in GitHub Desktop.
Bag Labs Post - In-App Purchases - Restoring Purchases Section
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Start by conforming to the SKPaymentTransactionObserver | |
//Restore button pressed | |
@IBAction func restorePurchasesBtnPressed(sender: AnyObject) | |
{ | |
SKPaymentQueue.defaultQueue().addTransactionObserver(self) | |
SKPaymentQueue.defaultQueue().restoreCompletedTransactions() | |
} | |
//MARK: SKPaymentTransactionObserver methods | |
func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) | |
{ | |
for transaction in transactions as! [SKPaymentTransaction] | |
{ | |
switch transaction.transactionState | |
{ | |
case SKPaymentTransactionState.Purchased: | |
println("Transaction completed successfully.") | |
SKPaymentQueue.defaultQueue().finishTransaction(transaction) | |
case SKPaymentTransactionState.Restored: | |
println("Transaction restored successfully.") | |
SKPaymentQueue.defaultQueue().finishTransaction(transaction) | |
case SKPaymentTransactionState.Failed: | |
println("Transaction Failed"); | |
//Handle the Error! | |
SKPaymentQueue.defaultQueue().finishTransaction(transaction) | |
default: | |
println(transaction.transactionState.rawValue) | |
} | |
} | |
} | |
func paymentQueue(queue: SKPaymentQueue!, restoreCompletedTransactionsFailedWithError error: NSError!) | |
{ | |
//Handle the Error! | |
println("Failed to restore in app purchases") | |
} | |
func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!) | |
{ | |
for transaction:SKPaymentTransaction in queue.transactions as! [SKPaymentTransaction] | |
{ | |
if transaction.payment.productIdentifier == "ProducId1" | |
{ | |
//Unlock Purchase 1 | |
} | |
else if transaction.payment.productIdentifier == "ProductId2" | |
{ | |
//Unlock Purchase 2 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment