恢复应用内购买Swift [英] Restoring In-App Purchase Swift

查看:109
本文介绍了恢复应用内购买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.

这是我的购买代码:

    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()

我看到了这种情况.Restored is注释掉但我不知道那里发生了什么。任何帮助都会非常感激。

I see that case .Restored is commented out but i don't know what goes in there. Any help would be really appreciated.

推荐答案

这取决于你在付款恢复时所做的事情。我在我的应用程序中所做的与我在 .Purchased 案例中所做的一样 - 我打开了隐藏的功能。在您的应用中,您可以关闭广告。事实上,在我的应用程序中, .Purchased .Restored 是相同的情况,你可能也可以这样做。这是我的一些代码:

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 ...
        }

这篇关于恢复应用内购买Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆