In App Purchase / Swift conversion

…衆ロ難τιáo~ 提交于 2020-01-01 17:06:14

问题


I'm trying to convert the tutorial In App Purchase into Swift ( http://www.tutorialspoint.com/ios/ios_in_app_purchase.htm )

I'm having some problems ... For the example below should not call "productsRequest" function? This does not happen! Should call or not?

Another problem is the line (function purchaseMyProduct), show me delegate error.. Does it have some problem in the declaration view?


    var defaultQueue: SKPaymentQueue = SKPaymentQueue.addTransactionObserver (self)

Thank you

Bonfs


Example:

import UIKit
import StoreKit

class ViewController: UITableViewController, SKProductsRequestDelegate,SKPaymentTransactionObserver {
    let produto_value =  "com.tutorialTest.testApp.testProduct"
    override func viewDidLoad() {
        super.viewDidLoad()
        self.fetchAvailableProducts() // call
    }

    func fetchAvailableProducts() {      
        let productID:NSSet = NSSet(object: self.produto_value);
        let productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID);
        productsRequest.delegate = self;
        productsRequest.start();
    }

    func productsRequest (request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
        println("here!")
        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.produto_value) {
                println(validProduct.localizedTitle)
                println(validProduct.localizedDescription)
                println(validProduct.price)
            } else {
                println(validProduct.productIdentifier)
            }
        } else {
            println("nothing")
        }      
    }

    func canMakePurchases() -> Bool
    {
        return SKPaymentQueue.canMakePayments()
    }

    func purchaseMyProduct(product: SKProduct) {
        if (self.canMakePurchases()) {
            var payment: SKPayment = SKPayment(product: product)
            var defaultQueue: SKPaymentQueue = SKPaymentQueue.addTransactionObserver(self)
            defaultQueue.addPayment(payment)
        } else {
            println("Purchases are disabled in your device")
        }
    }

    func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!)    {
        for transaction:AnyObject in transactions {
            if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
                switch trans.transactionState {
                case .Purchased:
                    SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
                    break;
                case .Failed:
                    SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
                    break;
                    // case .Restored:
                    //[self restoreTransaction:transaction];
                default:
                   break;
                }
           }
        }
    }

}

回答1:


Here's how you can add payment to the default queue. You can skip creating the temp variable.

SKPaymentQueue.defaultQueue().addPayment(payment)

Also for the default queue, you don't need to add the transaction observer every time you buy a product. I add the transaction observer for the default queue in my init.

SKPaymentQueue.defaultQueue().addTransactionObserver(self)



回答2:


Try the call as follows:

   func purchaseMyProduct(product: SKProduct) {
        if (self.canMakePurchases()) {
             println("Purchases are allowed ...")
            var payment: SKPayment = SKPayment(product: product)
            var defaultQueue: SKPaymentQueue  = SKPaymentQueue ()
            defaultQueue.addTransactionObserver(self)
            defaultQueue.addPayment(payment)
        } else {
           println("Purchases are disabled in your device")
        }
    }



回答3:


Swift 2.2

If you add the SKPaymentTransactionObserver you need to add at least this one function to deal with transactions.

func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {

    // deal  with the transactions here

}


来源:https://stackoverflow.com/questions/25015247/in-app-purchase-swift-conversion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!