Wednesday, October 28, 2026
HomeiOS Developmentios - saving merchandise to key chain will get not discovered error

ios – saving merchandise to key chain will get not discovered error

[ad_1]

Once I save gadgets to the keychain I get successful however after fetching it I get an error not discovered.

enum KeychainError: Error {
case itemNotFound
case duplicateItem
case invalidItemFormat
case unexpectedStatus(OSStatus)
}

last class KeyChainManager {
    
    static let service = "www.check.de"

    
static func save(password: String, account: String) throws {

    let passwordData = password.knowledge(utilizing: String.Encoding.utf8)!
    let question: [String: Any] = [
        kSecClass as String: kSecClassInternetPassword,
        kSecAttrAccount as String: account,
        kSecAttrServer as String: service,
        kSecValueData as String: passwordData
    ]

    let standing = SecItemAdd(
        question as CFDictionary,
        nil
    )

    if standing == errSecDuplicateItem {
        throw KeychainError.duplicateItem
    }
    guard standing == errSecSuccess else {
        throw KeychainError.unexpectedStatus(standing)
    }
    print("success")
}

static func readPassword( account: String) throws -> Knowledge {
    let question: [String: AnyObject] = [
        kSecAttrService as String: service as AnyObject,
        kSecAttrAccount as String: account as AnyObject,
        kSecClass as String: kSecClassGenericPassword,
        kSecMatchLimit as String: kSecMatchLimitOne,
        kSecReturnData as String: kCFBooleanTrue
    ]

    var itemCopy: AnyObject?
    let standing = SecItemCopyMatching(
        question as CFDictionary,
        &itemCopy
    )

    guard standing != errSecItemNotFound else {
        throw KeychainError.itemNotFound
    }
    

    guard standing == errSecSuccess else {
        throw KeychainError.unexpectedStatus(standing)
    }

    guard let password = itemCopy as? Knowledge else {
        throw KeychainError.invalidItemFormat
    }

    return password
  }
}

If I attempt

attempt KeyChainManager.save(password: enter.password ?? "", account: enter.username ?? "")

I get success but when I attempt to load the password it stops on this line
throw KeychainError.itemNotFound

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments