Search code examples
iosswiftnsuserdefaults

How to set UserDefaults time expire?


I used this code to store to UserDefaults. I want to know, how i can set time expire?

   static func _getByKey(_ key: String) -> Any? {
        UserDefaults.standard.string(forKey: key)
    }

    static func _setByKey(_ key: String, _ val: Any) {
        UserDefaults.standard.set(val, forKey: key)
    }

Solution

  • There is no short way to do so , you need to store a timestamp and check it with the current one every app open to clear the keys / not