Skip to content

Instantly share code, notes, and snippets.

@kristofer
Created August 3, 2016 11:55
Show Gist options
  • Save kristofer/9ce4ebce33a90dc080e4ba2c8b659d1c to your computer and use it in GitHub Desktop.
Save kristofer/9ce4ebce33a90dc080e4ba2c8b659d1c to your computer and use it in GitHub Desktop.
KeychainPasscodeRepository - a simple example of a ios keychain based repository for https://github.com/yankodimitrov/SwiftPasscodeLock
//
// KeychainPasscodeRepository.swift
// PasscodeLock
// a simple example of a keychain based repository
// https://github.com/yankodimitrov/SwiftPasscodeLock
// uses this fine work for easy access to Keychain
// https://cocoapods.org/pods/KeychainAccess
// written by https://github.com/kristofer
// this snip is a test/sample of the class usage.
// if true {
// let foo = KeychainPasscodeRepository()
// foo.deletePasscode()
//
// foo.savePasscode(["9","7","6","2"])
// let hasP = foo.hasPasscode
//
// let yaya = foo.passcode
//
// NSLog("\(hasP) \(yaya!)")
// }
class KeychainPasscodeRepository: PasscodeRepositoryType {
private let passcodeKey = "app.lock.passcode"
private lazy var keychain: Keychain = {
return Keychain(service: "com.something.App.PasscodeRepo")
}()
var hasPasscode: Bool {
if let foo = try? keychain.getString(passcodeKey) {
if foo == nil {
return false
}
return true
}
return false
}
var passcode: [String]? {
let pc = try! keychain.getString(passcodeKey)
var result = [String]()
for ch in pc!.characters {
result.append(String(ch))
}
return result
}
func savePasscode(passcode: [String]) {
let joindPasscode = passcode.joinWithSeparator("")
keychain[string: passcodeKey] = joindPasscode
}
func deletePasscode() {
do {
try keychain.remove(passcodeKey)
} catch let error {
NSLog("error: \(error)")
}
}
}
@ashwinnooli
Copy link

ashwinnooli commented Nov 22, 2016

Not working for me!!
Do you have an example ?

@minikin
Copy link

minikin commented Dec 8, 2016

@ashwinnooli you need to add KeychainAccess to your project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment