Decode/Encode ApplicationToken | Screen Time API features iOS 16

How I can decode ApplicationToken which I receive from FamilyActivitySelection?

I've been expecting String type or ability to extract String from ApplicationToken.

My goal is switching between user's modes, where one device could be used for Parent and Child. For this case I'd like to keep ManagedSettingsStore() setups for each user.

Answered by Boaz F. in 743598022

These are all codable so you can just do this:

var selectionToDiscourage: FamilyActivitySelection

if let encoded = try? JSONEncoder().encode(self.selectionToDiscourage) {
    UserDefaults.standard.set(encoded, forKey: defaultsRestrictionsKey)
}

Hi, I have the same question. Thanks in advance for the answer!

These things are Codable so you can work with them just like with any other standard data type. Also the FamilyActivitySelection is also Codable so you can store it in its entirety.

If you really want string, then you need to look at how to transform Data to one after you use JSONEncoder

Accepted Answer

These are all codable so you can just do this:

var selectionToDiscourage: FamilyActivitySelection

if let encoded = try? JSONEncoder().encode(self.selectionToDiscourage) {
    UserDefaults.standard.set(encoded, forKey: defaultsRestrictionsKey)
}
Decode/Encode ApplicationToken | Screen Time API features iOS 16
 
 
Q