Created
April 24, 2020 07:23
-
-
Save abadikaka/63a1876ab68a425ed07f65ac6ae633e6 to your computer and use it in GitHub Desktop.
Storage Kit SharingKitTutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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