Created
August 3, 2016 11:55
-
-
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
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
// | |
// 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)") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not working for me!!
Do you have an example ?