本文介绍了在App Purchase / Swift转换中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试将App App中的教程转换为Swift()


我遇到了一些问题...
对于下面的例子,不应该调用productsRequest函数?这不会发生!是否应该打电话?

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?

另一个问题是行(函数purchaseMyProduct),告诉我委托错误..在声明视图中是否有问题?

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)

谢谢

Bonfs

示例:

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

}


推荐答案

以下是如何将付款添加到默认队列的方法。您可以跳过创建临时变量。

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

SKPaymentQueue.defaultQueue().addPayment(payment)

同样对于默认队列,每次购买产品时都不需要添加事务观察器。我在init中为默认队列添加了事务观察器。

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)

这篇关于在App Purchase / Swift转换中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 16:39