0

I have two UIViewController. In the first one, I have a button that adds some views, one at a time, to the main view. In the second one, I set up a store, so that when I press on a button I unlock some features of my app. Now, I perfectly know (I hope) how to handle the part where I make the VCs comunicate and I trigger some other easy functions, what I don't know is how to make the store button increase the button's functions.

WHAT I NEED:
Right now the button adds a maximum of 10 views (complete version). I want that before the user buys my app, he gets to add a maximum of 3 views and then, when he buys it, the function I already have (the one to add 10 views)starts to work and replaces the other one.

MAIN VIEW CONTROLLER

var messageArray = [UIView] ()

I attached all of my UIView from the storyboard and I appended them to my array in the viewDid load like this: messageArray.append(View1)

@IBAction func addMessageViewButton(_ sender: Any) { 
    let hiddenViews = messageArray.filter { $0.isHidden }
    guard !hiddenViews.isEmpty else {

        let sheet = UIAlertController(title: "max reached", message: nil, preferredStyle: .actionSheet)
        let ok = UIAlertAction(title: "OK", style: .cancel, handler: nil)

        let closeAll = UIAlertAction(title: "Close all", style: .destructive) { (addMessage) in

      view1.isHidden = true
      view2.isHidden = true
      view3.isHidden = true
      view4.isHidden = true
      view5.isHidden = true
      view6.isHidden = true
      view7.isHidden = true
      view8.isHidden = true
      view9.isHidden = true
      view10.isHidden = true
    }
            sheet.addAction(ok)
            sheet.addAction(closeAll)

            present(sheet, animated: true, completion: nil)

            return

}
        let randomHiddenView = hiddenViews.randomElement()
        randomHiddenView?.isHidden = false
    }

SHOP VIEW CONTROLLER

Here I won't post all of the code because it would be too much and of course unnecessary, since the important thing to know here is that there's a button and if the user presses it and he proceeds with the purchase, he will get the function I posted up here working instead of the one that allows him to have just 3 views.

func unlock() {

        let appdelegate = UIApplication.shared.delegate
            as! AppDelegate
        appdelegate.viewController!.functionToHave10Views()
//viewControlled is declared in the app delegate like this ` var viewController: ViewController?`
//I know I don't physically have `functionToHave10Views()`, but I guess I'll turn the code of my button into a function, so just to be clear, I'm referring to that function.


    buyButton.isEnabled = false

}
2
  • 1
    Use a variable in your main view controller that acts depending on the unlock state. Commented May 12, 2019 at 21:07
  • Don't think about views or buttons directly. When the extra functionality is purchased you update your data model, whatever that is. You also need to persist that purchase information in a file or user defaults or the keychain. When the first view controller willAppear it can check the data model and update its views accordingly. Commented May 12, 2019 at 21:49

1 Answer 1

2

In your main view controller:

var isLocked = true

@IBAction func addMessageViewButton(_ sender: Any) {
  if isLocked {
    // Do something for when is locked
  } else {
    // Do something for when is unlocked
  }
}

Then in your shop view controller:

func unlock() {
    let appdelegate = UIApplication.shared.delegate as! AppDelegate
    appdelegate.viewController!.isLocked = false
    buyButton.isEnabled = false
}
Sign up to request clarification or add additional context in comments.

5 Comments

What if I want to save the var's value? I was trying with user defaults but I'm not really sure about how to store it, I tried to write var isLocked = UserDefaults.standard.bool(forKey: "ISLOCKEDKEY") and in the function in the other vc I then added defaults.set(false, ForKey: "ISLOCKEDKEY") but when I run the app at the beginning the var is set on false already, instead I want it to be on true
You can use the inverted logic. Check if the isUnlocked instead. So in the first time it will false and in when when you unlock you set it to true.
But that's the same: in fact if I write in my code var isUnlocked = false and in the other vc then I say it's true, the user will be forced to go each time in the store to restore the purchase in order to use it...
If I got your question correctly, it's not the same. The first time you run your app, you check using UserDefaults.standard.bool(forKey: "someKey") which will return false since it's the first opening of the app (no value stored yet). When the unlocking function executes, you set using UserDefaults.standard.set(true, forKey: "someKey"). In the subsequent checks you will get true, meaning it's unlocked.
Aaaah ok I got it, thank you a lot, I understood something else before. Again, thanks for the good answer and for your time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.