5 Store credential
mjunkmyjunk edited this page 2021-09-26 16:57:00 +08:00

You want certainly store token and token secret for next application run

You can store the two String attributes (oauth.client.credential. oauth_token & oauth_token_secret) or the OAuthSwiftCredential which conform to NSSecureCoding and Codable protocol

You can store it into keychain, coredata, sql database, encrypted file, etc...

Keychain

You can use one of the framework about keychain

Sample code with Prephirences

let keychain = KeychainPreferences.sharedInstance
// each element
keychain["the_token_key"] = oauth.client.credential.oauth_token
keychain["the_secret_token_key"] = oauth.client.credential.oauth_token_secret
// or credential (available in 2.0.3)
keychain["the_credential_key", .archive] = oauth.client.credential

To retrieve it

if let token = keychain["the_token_key"] as? String {..}
if let token_secret = keychain["the_secret_token_key"] as? String {..}
// or
if let credential = keychain["the_credential_key", .Archive] as? OAuthSwiftCredential,
            token = credential.oauth_token,
     token_secret = credential.oauth_token_secret {..}

Inject loaded token

oauth.client.credential.oauthToken = {your stored token}
oauth.client.credential.oauthTokenSecret = {your stored secret token}