2017-07-16 56 views
0

我有一個用Swift編寫的iOS應用程序,我在其自己的類(Store)中爲商店編寫代碼。然後我有一個視圖控制器(StoreController),它顯示產品詳細信息並呈現按鈕,以便用戶可以購買/恢復購買。我正在使用委託模式。我應該在哪裏應用程序運行時聲明/實例化Store對象以保持它可用?

我的問題是我應該在哪裏創建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 
    } 
} 
} 

回答

0

你說得對 - 實例在你的AppDelegate。你知道它只會被調用一次,所以它是初始化事物的好地方。

您可以從任何地方訪問的AppDelegate在您的應用程序

appDelegate = (UIApplication.shared.delegate as! AppDelegate) 
相關問題