0

The answer to this question may be obvious but I was wondering if it is possible to create an event such as an alert or popup when a view controller is segued to after X number of times e.g. the third time a view controller is loaded a PopUp appears, as I can't seem to find anything about this online.

The event should be a one off occurrence and once has been triggered then it should not happen again.

Ryan Hampton
  • 319
  • 1
  • 4
  • 21
  • You can save your count in userdefaults & can do whatever you want accessing that. – iYoung Dec 28 '16 at 14:46
  • Is this within one session or can the app be closed between these events? – Arc676 Dec 28 '16 at 14:48
  • How would I add the count to userDefaults? the app can be closed between the events – Ryan Hampton Dec 28 '16 at 14:50
  • buddy userdefaults holds the data even the app is closed or the device is restarted. You can check for X number & perform your action. – iYoung Dec 28 '16 at 14:52
  • So something like this: http://stackoverflow.com/questions/35379691/increment-integer-in-nsuserdefaults – Ryan Hampton Dec 28 '16 at 14:58
  • [`UserDefaults` documentation](https://developer.apple.com/reference/foundation/userdefaults) – Arc676 Dec 28 '16 at 14:59
  • I'm just curious about the User Experience. What is exactly is the pop up you are thinking? I may be a bad idea to do such entirely or is that you are just trying to learn something right now? – mfaani Dec 28 '16 at 15:54
  • Its an app where after they complete for example 3 workouts it asks them if they would like to leave a review. – Ryan Hampton Dec 28 '16 at 16:13

1 Answers1

1

Here is some sample code, how can u use NSUserDefaults to do something like you described.

You need the value stored by an identifier in NSUserDefaults, easiest is as an integer.

Whenever you call increaseViewLoadedCount(), it will increase the counter's value, e.g. a view has been loaded.

Whenever you call shouldShowAlert(), we read the value from defaults, check it against some value, and return a boolean based on that.

Also, the NSUserdefaults are being synchronised, therefore even if the app is terminated, the counter value will be kept.

extension UserDefaults {
    /// Indetifier of counter container in `defaults` 
    private static let kCounterIdentifier = "counter"

    /// Count of loads after the alert should not be loaded
    private static let notDisplayAlertAfterCount = 3

    /// Increase the counter value
    func increaseViewLoadedCount() {
        guard let counterValue = value(forKey: UserDefaults.kCounterIdentifier) as? Int else {
            syncronizeCounter(with: 1) 
            return
        }
        syncronizeCounter(with: counterValue + 1) 
    }

    /// Reset the value to zero in Userdaults
    func resetViewLoadedCount() {
        syncronizeCounter(with: 0)
    }

    /// Check weather the alert should be presented or not
    ///
    /// - Returns: true if counter is smaller than notDisplayAlertAfterCount, false if greater
    func shouldShowAlert() -> Bool {
        guard let counterValue = value(forKey: UserDefaults.kCounterIdentifier) as? Int else {
            return true
        }

        // Set the number anything you like
        return counterValue < UserDefaults.notDisplayAlertAfterCount ? true : false
    }

    private func syncronizeCounter(with value: Int) {
        set(value, forKey: UserDefaults.kCounterIdentifier)
        synchronize()
    }
}

Test:

print(defaults.shouldShowAlert()) // prints true
defaults.increaseViewLoadedCount()
defaults.increaseViewLoadedCount()
print(defaults.shouldShowAlert()) // prints true
defaults.increaseViewLoadedCount()
print(defaults.shouldShowAlert()) // prints false
dirtydanee
  • 6,081
  • 2
  • 27
  • 43
  • small note in your test, it should have two lots of loadManager.increaseViewLoadedCount() instead of 3 otherwise it prints false on the second call to shouldShowAlert – Ryan Hampton Dec 28 '16 at 16:30
  • @RyanHampton updated the solution using extension on `NSUserDefaults`. Should be more simple. – dirtydanee Dec 28 '16 at 16:47