I want to reload my ViewController
after a successful purchase. But how to do it from purchase class. How to reload ViewController
from another class?
class IAPObserver: NSObject, SKPaymentTransactionObserver {
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case .purchasing:
break
case .purchased:
delegate?.purchaseStatusDidUpdate(PurchaseStatus.init(state: .complete, error: nil, transaction: transaction, message:"Purchase Complete."))
SKPaymentQueue.default().finishTransaction(transaction)
// code to reload my ViewController
case .failed:
delegate?.purchaseStatusDidUpdate(PurchaseStatus.init(state: .failed, error: transaction.error, transaction: transaction, message:"An error occured."))
SKPaymentQueue.default().finishTransaction(transaction)
case .restored:
delegate?.restoreStatusDidUpdate(PurchaseStatus.init(state: .complete, error: nil, transaction: transaction, message:"Restore Success!"))
SKPaymentQueue.default().finishTransaction(transaction)
case .deferred:
break
}
}
}
You must call reloadData() in self, the view controller that manages the table view. Luckily, you can use notification to achieve calling the relevant function from another class. In the view controller
override func viewDidLoad() {
super.viewDidLoad()
...
NotificationCenter.default.addObserver(self, selector: #selector(reloadTableViewData), name: NSNotification.Name(rawValue: "reloadData"), object: nil)
}
@objc func reloadTableViewData() {
tableView.reloadData()
}
In your class IAPObserver, use the following to notify that you now want to reload data:
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadData"), object: nil)