Skip to content
Simple session written in Swift
Swift Shell Objective-C Ruby
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Demo/SimpleSessionDemo
Sources
.gitignore
LICENSE
README.md
SimpleSession.podspec

README.md

SimpleSession

Version Platform License

Índice

Features

  • Easy to use
  • Personalize session protocol
  • Default session protocol use user defaults

Prerequisites

  • iOS 8+
  • Xcode 7+
  • Swift 3.0

Installation

SimpleSession is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "SimpleSession"

How to Use

Check out the demo project for a concrete example.

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()

API

Default values

Session protocol. Default use user defaults

SimpleSession.SESSION_PROTOCOL: SimpleSessionProtocol = UserDefaultsSession.sharedInstance // Default session use user defaults

// Configure other session
SimpleSession.SESSION_PROTOCOL = MySession()
get
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
put
SimpleSession.put(key: String, value: Any?)

Put value for key in session. Expired in seconds.

SimpleSession.put("a.key", value: 10)
has
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
remove
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
clear

Return remove all keys in session.

SimpleSession.clear()

Personalize

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
    }

    // ...

}

License

SimpleSession is available under the MIT license. See the LICENSE file for more info.

You can’t perform that action at this time.