我正在使用鑰匙串來保存持久數據。 的功能2鑰匙串持久數據iOS
func savePassword(password: String, inKeychainItem: NSData?) -> NSData?
func getPasswordWithPersistentReference(persistentReference: NSData) -> String?
我只是想確保,如果我使用savePassword功能,並得到一個NSData的,我應該在哪裏保持這個NSData的保持它持續到下一次,我會運行應用程序嗎? userDefault是保存這些數據的安全場所嗎?
編輯:這些都是我使用
func savePassword(password: String, inKeychainItem: NSData?) -> NSData? {
guard let passwordData = password.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) else { return nil }
var status = errSecSuccess
if let persistentReference = inKeychainItem {
// A persistent reference was given, update the corresponding keychain item.
let query: [NSObject: AnyObject] = [
kSecValuePersistentRef : persistentReference,
kSecReturnAttributes : kCFBooleanTrue
]
var result: AnyObject?
// Get the current attributes for the item.
status = SecItemCopyMatching(query, &result)
if let attributes = result as? [NSObject: AnyObject] where status == errSecSuccess {
// Update the attributes with the new data.
var updateQuery = [NSObject: AnyObject]()
updateQuery[kSecClass] = kSecClassGenericPassword
updateQuery[kSecAttrService] = attributes[kSecAttrService]
var newAttributes = attributes
newAttributes[kSecValueData] = passwordData
status = SecItemUpdate(updateQuery, newAttributes)
if status == errSecSuccess {
return persistentReference
}
}
}
func getPasswordWithPersistentReference(persistentReference: NSData) -> String? {
var result: String?
let query: [NSObject: AnyObject] = [
kSecClass : kSecClassGenericPassword,
kSecReturnData : kCFBooleanTrue,
kSecValuePersistentRef : persistentReference
]
var returnValue: AnyObject?
let status = SecItemCopyMatching(query, &returnValue)
if let passwordData = returnValue as? NSData where status == errSecSuccess {
result = NSString(data: passwordData, encoding: NSUTF8StringEncoding) as? String
}
return result
}
EDIT2功能:基本上,我只是想檢查我是否保存密碼,如果我這樣做,然後我想要得到它。爲此,我需要一個persistentReference。我從保存密碼功能中獲得一個。但我應該在哪裏保留它?
這些功能不是iOS SDK的一部分。如果您需要使用它們的幫助,則必須編輯您的問題以包含它們的源代碼。 –
我認爲這些函數的返回類型應該足夠了,但是會編輯 – Roee84