Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restoring In-App Purchase Swift

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.

like image 990
PoisonedApps Avatar asked Jan 12 '15 01:01

PoisonedApps


1 Answers

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 ...
        }
like image 197
matt Avatar answered Sep 27 '22 22:09

matt