gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:18:19 25 4
gpt4 key购买 nike

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

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

当前的问题是,如果我测试它并导航到 StoreController View (通过 Store 发起请求),然后按“返回”,应用程序就会卡住。我想是因为它正在等待响应?

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

这是我的 Store 类:

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( View Controller ):

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

最佳答案

你是对的 - 在你的 AppDelegate 中实例化它。您知道它将被调用一次且仅一次,因此这是初始化事物的好地方。

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

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

关于ios - 我应该在哪里声明/实例化我的 Store 对象以使其在应用程序运行时可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45127278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com