Skip to content

Instantly share code, notes, and snippets.

@abadikaka
Created April 24, 2020 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abadikaka/63a1876ab68a425ed07f65ac6ae633e6 to your computer and use it in GitHub Desktop.
Save abadikaka/63a1876ab68a425ed07f65ac6ae633e6 to your computer and use it in GitHub Desktop.
Storage Kit SharingKitTutorial
//
// StorageKit.swift
// StorageKitTutorial
//
// Created by Michael Abadi Santoso on 1/27/20.
// Copyright © 2020 Michael Abadi Santoso. All rights reserved.
//
import Foundation
public let appGroupIdentifier = "group.com.michaelabadi.ShareKitTutorial.container"
/// StorageKit task is loading and saving any kind of token authentification
public class StorageKit {
public enum StorageType {
case `default`
case sharing
}
private let type: StorageType
public init(type: StorageType) {
self.type = type
}
/// Function for saving the token.
/// - Parameter token: the existing token.
public func saveToken(_ token: String) {
getUserDefault()?.set(token, forKey: "token")
}
/// Function for loading the token.
public func loadToken() -> Any? {
return getUserDefault()?.value(forKey: "token")
}
/// Function for delete the existing token.
public func deleteToken() {
getUserDefault()?.removeObject(forKey: "token")
}
private func getUserDefault() -> UserDefaults? {
switch type {
case .default:
return UserDefaults.standard
case .sharing:
return UserDefaults(suiteName: appGroupIdentifier)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment