I have added a non-consumable in-app purchase to my app to remove the ads. The purchasing works fine, its the restoring iam having trouble with. I just don't know what to do.
Here is my purchasing code:
func buyNonConsumable(){
println("About to fetch the products");
// We check that we are allow to make the purchase.
if (SKPaymentQueue.canMakePayments())
{
var productID:NSSet = NSSet(object: self.product_id!);
var productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID);
productsRequest.delegate = self;
productsRequest.start();
println("Fething Products");
}else{
println("can't make purchases");
}
}
// Helper Methods
func buyProduct(product: SKProduct){
println("Sending the Payment Request to Apple");
var payment = SKPayment(product: product)
SKPaymentQueue.defaultQueue().addPayment(payment);
}
func removeTheAds(){
userDefaults.setBool(true, forKey: "proUser")
userDefaults.synchronize()
print("You are a pro User")
//Code for removing the ads currently
}
// Delegate Methods for IAP
func productsRequest (request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
println("got the request from Apple")
var count : Int = response.products.count
if (count>0) {
var validProducts = response.products
var validProduct: SKProduct = response.products[0] as SKProduct
if (validProduct.productIdentifier == self.product_id) {
println(validProduct.localizedTitle)
println(validProduct.localizedDescription)
println(validProduct.price)
buyProduct(validProduct);
} else {
println(validProduct.productIdentifier)
}
} else {
println("no product found")
}
}
func request(request: SKRequest!, didFailWithError error: NSError!) {
println("There was an error");
}
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)
removeTheAds()
break;
case .Failed:
println("Purchased Failed");
SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
break;
// case .Restored:
default:
SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
break;
}
}
}
}
How do i handle restores? I have a restore button that calls both of these but what do i do now?
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
I see that case .Restored is commented out but i don't know what goes in there. Any help would be really appreciated.
It's up to you what you do when a payment is restored. What I do in my app is the same thing I do in the .Purchased
case - I switch on the hidden functionality. It looks like in your app, you'd turn off the ads. In fact, in my app, .Purchased
and .Restored
are the same case, and you could probably do the same. This is a little of my code:
switch t.transactionState {
case .Purchased, .Restored:
// ... turn on the magic ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With