-5.2 C
New York
Wednesday, January 15, 2025

ios – Unable to fetch key from Keychain in Swift


Strive with this code. I’m utilizing this one and it really works nice.

class KeychainAccess {
    
    func addKeychainData(itemKey: String, itemValue: String) throws {
        guard let valueData = itemValue.knowledge(utilizing: .utf8) else {
            print("Keychain: Unable to retailer knowledge, invalid enter - key: (itemKey), worth: (itemValue)")
            return
        }
        
        //delete outdated worth if saved first
        do {
            attempt deleteKeychainData(itemKey: itemKey)
        } catch {
            print("Keychain: nothing to delete...")
        }
        
        let queryAdd: [String: AnyObject] = [
            kSecClass as String: kSecClassGenericPassword,
            kSecAttrAccount as String: itemKey as AnyObject,
            kSecValueData as String: valueData as AnyObject,
            kSecAttrAccessible as String: kSecAttrAccessibleWhenUnlocked
        ]
        let resultCode: OSStatus = SecItemAdd(queryAdd as CFDictionary, nil)
        
        if resultCode != 0 {
            print("Keychain: worth not added - Error: (resultCode)")
        } else {
            print("Keychain: worth added efficiently")
        }
    }
    
    func deleteKeychainData(itemKey: String) throws {
        let queryDelete: [String: AnyObject] = [
            kSecClass as String: kSecClassGenericPassword,
            kSecAttrAccount as String: itemKey as AnyObject
        ]
        
        let resultCodeDelete = SecItemDelete(queryDelete as CFDictionary)
        
        if resultCodeDelete != 0 {
            print("Keychain: unable to delete from keychain: (resultCodeDelete)")
        } else {
            print("Keychain: efficiently deleted merchandise")
        }
    }
    
    func queryKeychainData (itemKey: String) throws -> String? {
        let queryLoad: [String: AnyObject] = [
            kSecClass as String: kSecClassGenericPassword,
            kSecAttrAccount as String: itemKey as AnyObject,
            kSecReturnData as String: kCFBooleanTrue,
            kSecMatchLimit as String: kSecMatchLimitOne
        ]
        var consequence: AnyObject?
        let resultCodeLoad = withUnsafeMutablePointer(to: &consequence) {
            SecItemCopyMatching(queryLoad as CFDictionary, UnsafeMutablePointer($0))
        }
        
        if resultCodeLoad != 0 {
            print("Keychain: unable to load knowledge - (resultCodeLoad)")
            return nil
        }
        
        guard let resultVal = consequence as? NSData, let keyValue = NSString(knowledge: resultVal as Knowledge, encoding: String.Encoding.utf8.rawValue) as String? else {
            print("Keychain: error parsing keychain consequence - (resultCodeLoad)")
            return nil
        }
        return keyValue
    }
}

Create one object and use that object to retailer and fetch.

let keychain = KeychainAccess()

     do {
          attempt? self.keychain.addKeychainData(itemKey: "KeyChainUserName", itemValue: consequence?.buyer?.e-mail ?? "")
 } catch {
     print("error of consumer Identify")
 }

do {
     attempt? self.keychain.addKeychainData(itemKey: "KeyChainPassword", itemValue: "(consequence?.buyer?.id ?? 0)" )
  } catch {
          print("error of password")
}

Fetch knowledge from the Keychain.

let userName = attempt? self.keychain.queryKeychainData(itemKey: "KeyChainUserName")
print("UserName:", userNam)

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles