Sam
Sam

Reputation: 145

In-app-purchase call function when successful

I have a button which purchases a product which should hide the background view shielding the user from the using the extension to the app. When the button is pressed, the code to buy the product is from an external file. Because of this, I have know way of hiding the view when the purchase comes back successful.

Code from other file

for transaction: AnyObject in transactions {
        if let trans: SKPaymentTransaction = transaction as? SKPaymentTransaction {
            switch trans.transactionState {
            case .purchased:
                print("Product Purchased")
                let purchased = UserDefaults.standard.bool(forKey: 
 "Analytics")
                UserDefaults.standard.set(true, forKey: "Analytics")

Is it possible to call a function to a view controller class from an external swift file? What other way could you approach the problem?

Upvotes: 1

Views: 180

Answers (1)

carbonr
carbonr

Reputation: 6067

You can use blocks or delegates to pass information back.

Inside the PaymentClass subclass declare a delegate

protocol PaymentDelegate {
  func payment(completed: Bool)
}

class YourExternalPaymentClass {
  weak var delegate: PaymentDelegate?
}

Now in the ViewController where you are showing the overlay configure this delegate

Inside the payment class where the event is completed, call your method

   self.delegate?.payment(completed: true)

this way the data can be passed back to the ViewController and you can hide or show whatever you want.

Upvotes: 1

Related Questions