Last active
January 1, 2017 04:09
-
-
Save benbahrenburg/657dc0aa948504566bd51054e6bfda00 to your computer and use it in GitHub Desktop.
Use the Keychain to determine if the device has a passcode set.
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
@available(iOS 8.0, *) | |
public func devicePasscodeEnabledUsingKeychain() -> Bool { | |
let query: [String:Any] = [ | |
kSecClass as String : kSecClassGenericPassword, | |
kSecAttrAccount as String : UUID().uuidString, | |
kSecAttrAccessible as String: kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly, | |
kSecValueData as String: "HelloWorld".data(using: String.Encoding.utf8)!, | |
kSecReturnAttributes as String : kCFBooleanTrue | |
] | |
var dataTypeRef: AnyObject? | |
var status = withUnsafeMutablePointer(to: &dataTypeRef) { SecItemCopyMatching(query as CFDictionary, UnsafeMutablePointer($0)) } | |
if status == errSecItemNotFound { | |
let createStatus = SecItemAdd(query as CFDictionary, nil) | |
guard createStatus == errSecSuccess else { return false } | |
status = withUnsafeMutablePointer(to: &dataTypeRef) { SecItemCopyMatching(query as CFDictionary, UnsafeMutablePointer($0)) } | |
} | |
guard status == errSecSuccess else { return false } | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment