In my app, there is a button in the SubscriptionVC that, when pushed, will call the 'purchase' function from the IAPService class. But after the function is completed I wish to change the elements in the ViewController using the ViewController's switchState() method. Does anyone know how to go about this?
class SubscriptionVC: UIViewController{
@IBOutlet weak var slideBtn: UISegmentedControl!
@IBOutlet weak var testLbl: UILabel!
func switchSubscribeState(){
testLbl.text = “You have changed this label”
slideBtn.isHidden=true
}
@IBAction func touchSubscribeBtn(_ sender: Any) {
TestService.shared.buttonPushed( subsriptionVC:self, product: prod)
}
class IAPService: NSObject {
private override init() {}
static let shared = IAPService()
var currentViewController: SubscriptionVC?
func purchase(subsriptionVC: SubscriptionVC ) {
currentViewController = subsriptionVC
guard let productToPurchase = products.filter({ $0.productIdentifier == product.rawValue}).first else {return}
let payment = SKPayment(product: productToPurchase)
print("payment",product.rawValue)
paymentQueue.add(payment)
}
}
extension IAPService: SKPaymentTransactionObserver {
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case .failed, .deferred:
SKPaymentQueue.default().finishTransaction(transaction)
SKPaymentQueue.default().remove(self)
break
case .purchasing:
break
case .purchased,.restored:
//Call switchState() when purchased
subVC?.switchSubscribeState()
SKPaymentQueue.default().finishTransaction(transaction)
SKPaymentQueue.default().remove(self)
break
default:
SKPaymentQueue.default().finishTransaction(transaction)
SKPaymentQueue.default().remove(self)
}
}
}
}
This is a case for the "Delegate Pattern"
(It looks like you use an intermediate class called TestService, so you might have to use that class to implement the delegate or refactor your code)