在应用程序运行时,我应该在哪里声明/实例化我的Store对象以使其可用?

时间:2017-07-16 10:00:03

标签: ios swift delegates storekit

我有一个用Swift编写的iOS应用程序,我在其中为自己的类(Store)编写了商店代码。然后我有一个视图控制器(StoreController),它显示产品详细信息并显示按钮,以便用户可以购买/恢复购买。我正在使用委托模式。

我的问题是我应该在哪里创建Store对象?目前我将它声明为StoreController的一个属性,我认为这是错误的。我希望能够在我的应用加载时创建它并保留它直到它退出。

目前的问题是,如果我测试它并导航到StoreController视图(通过Store启动请求),然后按'返回'该应用程序冻结。我假设是因为它在等待回应?

我在网上看到一个例子,有人在AppDelegate文件中创建了它,但这对我没有用,因为我无法从StoreController引用它。

这是我的商店类:

import Foundation
import StoreKit

protocol ClassStoreDelegate: class {
func storeUpdateReceived(store: Store)
}

class Store: NSObject, SKProductsRequestDelegate, SKPaymentTransactionObserver {

// Properties
weak var delegate: ClassStoreDelegate?
var list = [SKProduct]()
var p = SKProduct()
var defaults = UserDefaults.standard // Where we save whether the user is pro
var localTitle: String?
var localDescription: String?
var localPrice: String?

// Methods
// Calling the delegate method
func storeUpdate() {
    delegate?.storeUpdateReceived(store: self)
}

// Buy the product
func buy() {
    for product in list {
        let prodID = product.productIdentifier
        if(prodID == "com.squidgylabs.pro") {
            p = product
            buyProduct()
        }
    }
}

// Restore products
func restore() {
    SKPaymentQueue.default().add(self)
    SKPaymentQueue.default().restoreCompletedTransactions()
}


func getProducts() {
    if(SKPaymentQueue.canMakePayments()) {
        let productID: NSSet = NSSet(objects: "com.squidgylabs.pro")
        let request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>)
        request.delegate = self
        request.start()
    } else {
        delegate?.storeUpdateReceived(store: self)
    }
}

func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
    let myProduct = response.products
    for product in myProduct {
        list.append(product)
    }
    // Update labels
    localTitle = list[0].localizedTitle
    localDescription = list[0].localizedDescription

    // Format the price and display
    let formatter = NumberFormatter()
    formatter.locale = Locale.current
    formatter.numberStyle = .currency
    if let formattedPrice = formatter.string(from: list[0].price){
        localPrice = ("buy \(formattedPrice)")
        delegate?.storeUpdateReceived(store: self)
    }
}

func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
    let transactionsArray = queue.transactions
    if (transactionsArray.isEmpty) {
        delegate?.storeUpdateReceived(store: self)
    }
    else {
        for transaction in transactionsArray {
            let t: SKPaymentTransaction = transaction
            let prodID = t.payment.productIdentifier as String

            switch prodID {
            case "com.squidgylabs.pro":
                defaults.set(true, forKey: "pro")
                delegate?.storeUpdateReceived(store: self)
            default:
                delegate?.storeUpdateReceived(store: self)
            }
        }
    }
}

func buyProduct() {
    let pay = SKPayment(product: p)
    SKPaymentQueue.default().add(self)
    SKPaymentQueue.default().add(pay as SKPayment)
}

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
    for transaction: AnyObject in transactions {
        let trans = transaction as! SKPaymentTransaction

        switch trans.transactionState {
        case .purchased:
            let prodID = p.productIdentifier
            switch prodID {
            case "com.squidgylabs.pro":
                defaults.set(true, forKey: "pro")
                delegate?.storeUpdateReceived(store: self)
            default:
                        delegate?.storeUpdateReceived(store: self)
            }
            queue.finishTransaction(trans)
            break
        case .failed:
            delegate?.storeUpdateReceived(store: self)
            queue.finishTransaction(trans)
            break
        case .restored:
            SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
            queue.finishTransaction(trans)
            break
        default:
            break
        }
    }
}

}

这是我的StoreController(视图控制器):

import UIKit

class StoreController: UIViewController, ClassStoreDelegate   {

// Properties
let store = Store()

// Outlets
@IBOutlet weak var localTitle: UILabel!
@IBOutlet weak var localDescription: UILabel!
@IBOutlet weak var buy: UIButton!
@IBOutlet weak var restore: UIButton!

// Actions
@IBAction func buy(_ sender: UIButton) {
    print("buy pressed")
    store.buy()
}

@IBAction func restore(_ sender: UIButton) {
    print("restore pressed")
    store.restore()
}

// Methods
override func viewDidLoad() {
    super.viewDidLoad()
    store.delegate = self // bind the delegate like this?

    // Get list of products for the store
    localTitle.isEnabled = false
    localDescription.isEnabled = false
    buy.isEnabled = false
    restore.isEnabled = false
    self.navigationItem.title = "Store"
    // update once the list of products is got from the store object
    store.getProducts()
}

// Running the delegate update
func storeUpdateReceived(store: Store) {
    print("storeUpdateReceived activated")
    if ((store.localTitle) != nil) {
        localTitle.text = store.localTitle!
        localDescription.text = store.localDescription
        buy.setTitle(store.localPrice, for: .normal)
        localTitle.isEnabled = true
        localDescription.isEnabled = true
        buy.isEnabled = true
        restore.isEnabled = true
    }
}
}

1 个答案:

答案 0 :(得分:0)

你是对的 - 在你的AppDelegate中实例化它。你知道它只会被调用一次,所以它是初始化事物的好地方。

您可以从应用中的任何位置访问AppDelegate

appDelegate = (UIApplication.shared.delegate as! AppDelegate)