- Easy to use
- Personalize session protocol
- Default session protocol use user defaults
- iOS 8+
- Xcode 7+
- Swift 3.0
SimpleSession is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "SimpleSession"
Check out the demo project for a concrete example.
// String
SimpleSession.put("string", value: "This is a string")
print("Key: string\nValue: \(SimpleSession.get("string"))")
// Number
SimpleSession.put("number", value: 20.3)
print("Key: number\nValue: \(SimpleSession.get("number"))")
// JSON
SimpleSession.put("json", value: ["key": "value"])
print("Key: json\nValue: \(SimpleSession.get("json"))")
// NSData
SimpleSession.put("data", value: NSData(bytes: [0xFF, 0xD9] as [UInt8], length: 2))
print("Key: data\nValue: \(SimpleSession.get("data"))")
print("------------------------------")
// Remove an object from the session
SimpleSession.remove("string")
print("Remove Key: string\nValue: \(SimpleSession.get("string"))")
print("------------------------------")
// Clean the session
SimpleSession.clear()
Session protocol. Default use user defaults
SimpleSession.SESSION_PROTOCOL: SimpleSessionProtocol = UserDefaultsSession.sharedInstance // Default session use user defaults
// Configure other session
SimpleSession.SESSION_PROTOCOL = MySession()
SimpleSession.get(key: String, defaultValue: Any? = nil)
Return value for key
in session. If not has key
in session return defaultValue
.
SimpleSession.get("exist.key") // return value for "exist.key"
SimpleSession.get("not.exist.key") // return nil, because "not.exist.key" not exist in session
SimpleSession.get("not.exist.key2", defaultValue: 10) // return 10, because "not.exist.key" not exist in session, but defaultValue is set
SimpleSession.put(key: String, value: Any?)
Put value
for key
in session. Expired in seconds
.
SimpleSession.put("a.key", value: 10)
SimpleSession.has(key: String)
Return true
if has value for key
in session, else false
.
SimpleSession.has("exist.key") // return true
SimpleSession.has("not.exist.key") // return false
SimpleSession.remove(key: String)
Return value for key
in session if exist, and remove this key in session.
SimpleSession.remove("exist.key") // return value for "exist.key"
SimpleSession.remove("not.exist.key") // return nil, because "not.exist.key" not exist in session
Return remove all keys in session.
SimpleSession.clear()
import SimpleSession
open class MySession: SimpleSessionProtocol
{
fileprivate var session: [String : Any] = [:]
open func get(_ key: String, defaultValue: Any? = nil) -> Any? {
if let value = session[key] {
return value
}
return defaultValue
}
open func put(_ key: String, value: Any?) {
session[key] = value
}
open func has(_ key: String) -> Bool {
return get(key) != nil
}
@discardableResult open func remove(_ key: String) -> Any? {
return session.removeValueForKey(key)
}
}
// Configure in AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {
// ...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Configure session
SimpleSession.SESSION_PROTOCOL = MySession()
return true
}
// ...
}
SimpleSession is available under the MIT license. See the LICENSE file for more info.