Swiftpack.co - ashleymills/Keychain.swift as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by ashleymills.
ashleymills/Keychain.swift v4.1.0
A simple type-safe drop in Swift wrapper class for the keychain
⭐️ 73
🕓 1 year ago
iOS macOS
.package(url: "https://github.com/ashleymills/Keychain.swift.git", from: "v4.1.0")

Simple-KeychainSwift

Version License Platform

Supporting Reachability.swift

Keeping Simple-KeychainSwift up-to-date is a time consuming task. Making updates, reviewing pull requests, responding to issues and answering emails all take time. If you'd like to help keep me motivated, please download my free app, Photo Flipper from the App Store. (To really motivate me, pay $1.99 for the IAP 😀)

And don't forget to ★ the repo. This increases its visibility and encourages others to contribute.

Thanks Ash

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

Manual

Just drop the Keychain.swift file into your project. That's it!

Swift Package Manager

Simple-KeychainSwift is available through Swift Package Manager. Xcode 11.0+ is required. To install, open Xcode -> File -> Swift Packages -> Add Package Dependency and paste repo's address

https://github.com/ashleymills/Keychain.swift

CocoaPods

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

pod "Simple-KeychainSwift"

Author

Simple-KeychainSwift was wrtten by Ashley Mills, [email protected]

License

Simple-KeychainSwift is available under the MIT license. See the LICENSE file for more info.

Example usage

Simple-KeychainSwift declares a protocol TypeSafeKeychainValue:

public protocol TypeSafeKeychainValue {
    func data() -> Data?                   // Convert to Data
    static func value(data: Data) -> Self? // Convert from Data
}

You can use Simple-KeychainSwift to set any types that conform to this protocol. Currently supported are String, Int, Bool and Date, To set other types, add conformity to TypeSafeKeychainValue, e.g.

extension Int: TypeSafeKeychainValue {
    public func data() -> Data? {
        var value = self
        return Data(bytes: &value, count: MemoryLayout.size(ofValue: value))
    }
    public static func value(data: Data) -> Int? {
        return data.withUnsafeBytes { $0.pointee }
    }
}

Set a key/value pair

Keychain.set("some value", forKey: "some string")
Keychain.set(true, forKey: "some bool")
Keychain.set(Date(), forKey: "some date")
Keychain.set(27, forKey: "some int")

Retrieve the value for a key

Keychain.value(forKey: "some string") as String
Keychain.value(forKey: "some bool") as Bool
Keychain.value(forKey: "some date") as Date
Keychain.value(forKey: "some int") as Int

Delete a key/value pair

Keychain.removeValue(forKey: "my key")

Delete all values from the keychain

Keychain.reset()

Want to help?

Got a bug fix, or a new feature? Create a pull request and go for it!

Let me know!

If you use Simple-KeychainSwift, please let me know.

Cheers, Ash

GitHub

link
Stars: 73
Last commit: 1 year ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Related Packages

Release Notes

5.0.0
1 year ago

Add macOS(.v13) platform Update iOS platform, to .iOS(.v11)

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics