I’m building a Shopify app, but first and foremost this is NOT a Shopify SDK question, it’s regarding saving a customer token into userDefaults or Keychain. I’ve tried but most Keychain libraries I’ve tried only work with strings and I need to save the token as a whole which looks like this:
BUYCustomerToken(customerID: NSNumber!, accessToken: String!, expiry: Date!)
The shared client of the app to access the token looks like this:
BUYClient.sharedClient.customerToken
During sign up, user gets created successfully with token. Once I exit and go back into the app the
BUYClient.sharedClient.customerToken = ""
So my question is, how do I save this object into defaults or Keychain to recall later when entering app. I can’t just save part of the token, like the accessToken which is just a String, I need the whole thing. Thanks in advance!
2
Answers
You can save more than String types in UserDefaults, you have to store your token in multiple keys.
https://developer.apple.com/reference/foundation/userdefaults
if you want to store the expiry date
The CustomerID
And for the token the same.
With this way, you just have to get them one by one, but you have all of them stored. I hope it helps you!
You definitely can store the whole object in your
UserDefaults
or a Keychain, no problem. The trick is to make it conform to theNSCoding
protocol, then useNSKeyedArchiver
andNSKeyedUnarchiver
to convert it to and from aData
object that can be stored. Don’t worry, it sounds more complicated than it is.Conforming to NSCoding
Presumably your
BUYCustomerToken
class is already a descendant ofNSObject
, so you’re halfway there. You just need to add a couple of methods to allow your custom class to be encoded (turned intoData
) and decoded automatically from anNSCoder
. Specifically these are:encode(with coder: NSCoder)
andinit(coder aDecoder:NSCoder)
. You then use the coder’s various encode() and decode() functions to make these work. encode() and decode() work on any object that supportsNSCoding
, which includesNSNumber
,String
, andDate
, so that makes your job here pretty easy. A finished codable class will look like this:You can add in your own custom methods, of course. The
description
is not strictly necessary, but convenient for testing.Storing and Retrieving from UserDefaults
Now that your class supports NSCoding, you can archive it and store it, then retrieve it.
To store it, you start by archiving it (converting it to a
Data
object) usingNSKeyedArchiver.archivedData(withRootObject)
. This works because your class isNSCoding
compliant. Once you get aData
object, you can store that inUserDefaults
usingset(value, forKey)
.When you want to retrieve it, you do it the other way: first fetch a
Data
object fromUserDefaults
usingdata(forKey)
, then turn it back into an object usingNSKeyedUnarchiver.unarchiveObject(with)
, and finally cast it to your custom class.Here’s a bit of code that attempts to load a
BUYCustomerToken
fromUserDefaults
. If it succeeds, it prints the description; if it fails, it creates a new token and stores it. Put this code in theviewDidLoad()
of your initialUIViewController
to see it in action:The first time you run it there’s no stored token, so the output is:
The second time, it will find the stored token and output its description: