0

Trying to implement/learn keychain use in iOS. I have saved multiple values of a struct in Keychain but while retrieving values it is showing nil. Testing the code in Simulator only. This is the generic keychain read method:

        extension KeychainHelper{         
        func read<T>(service: String, account: String, type: T.Type) -> T? where T : Codable {
        // Read item data from keychain
        guard let data = read(service: service, account: account) else {
        return nil
        }

         // Decode JSON data to object
        do {
            let item = try JSONDecoder().decode(type, from: data)
            return item
           } catch {
           assertionFailure("Fail to decode item for keychain: \(error)")
           return nil
        }}}

The full code of KeychainHelper class can be found here full keychainhelper class (can be replicated easily) when I am calling the API the code is working fine showing values also

         class ViewModel : ObservableObject{
          
          //some published var like centerService
          
          let token = response.ResponseData.AuthToken
          //other var
          
           let userDetails = Pass(authToken:token, other vars)
           KeychainHelper.standard.save(userDetails,service,account)
         }

But when I am trying to read keychain from different func it is showing nil. This is the call:

     class checkStatus{
        var userStatus = ViewModel()  
        let userDetails = KeychainHelper.standard.read(service: userStatus.centerService, 
        account:userStatus.centerAccount, Pass.self)
              }

The error is Operation failed with status -25299. Pass definition:

     struct Pass:Codable{
      let authToken:String
      let centerID:String
      let userID:String
      let accessID:Int
       }
Asperi
  • 228,894
  • 20
  • 464
  • 690
tintin
  • 335
  • 2
  • 8

1 Answers1

1

This error means that an item with this identifier already exists: OSStatus codes Try deleting it from the keychain . You can also check other answers in similar SO question

Mr.SwiftOak
  • 1,469
  • 3
  • 8
  • 19
  • deleted than again saved the credentials same issue finished with status -25299 it means save func is working correctly but not read.... why it is failing to read it I dont want to write again and again...once it is stored it should be readable – tintin May 16 '22 at 10:59
  • Did the deletion of keychain item ended with succcess status code? Anyway, I don't personally like apple keychain API, so If you don't mind adding another dependency into your project, I can only recommend using [Keychainswift library](https://github.com/evgenyneu/keychain-swift) which is more easy to use and user friendly. (and no need to use unsafe swift) – Mr.SwiftOak May 16 '22 at 11:13
  • yes status was 0.....all values, which I am trying to save and read, returned nil on first run after that same prob unable to read....this is frustrating – tintin May 16 '22 at 11:23
  • and trying this form of saving password? : [DAKechainn class](https://github.com/dagostini/DAKeychain/blob/master/DAKeychain/Classes/DAKeychain.swift) – Mr.SwiftOak May 16 '22 at 11:35