Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Keychain in Mac Catalyst app on Mac?

I can't write/read from Keychain in a Mac-Catalyst app on Mac, it returns errors 34018 and 25300 respectively. Is there a way to make Keychain work on Mac in a Catalyst app?

Xcode: 11.0, MacOS: 10.15

Here is a sample code, it works on iOS but not on Mac. The code prints "My secretive bee 🐝" to indicate that we have successfully written this text to Keychain and then read from it.

override func viewDidLoad() {
  super.viewDidLoad()

  let itemKey = "My key"
  let itemValue = "My secretive bee 🐝"
  deleteFromKeychain(itemKey: itemKey)
  addToKeychain(itemKey: itemKey, itemValue: itemValue)
  readFromKeychain(itemKey: itemKey)
}

func deleteFromKeychain(itemKey: String) {
  let queryDelete: [String: AnyObject] = [
    kSecClass as String: kSecClassGenericPassword,
    kSecAttrAccount as String: itemKey as AnyObject,
  ]

  let resultCodeDelete = SecItemDelete(queryDelete as CFDictionary)

  if resultCodeDelete != noErr {
    print("Error deleting from Keychain: \(resultCodeDelete)")
  }
}

func addToKeychain(itemKey: String, itemValue: String) {
  guard let valueData = itemValue.data(using: String.Encoding.utf8) else {
    print("Error saving text to Keychain")
    return
  }

  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 = SecItemAdd(queryAdd as CFDictionary, nil)

  if resultCode != noErr {
    print("Error saving to Keychain: \(resultCode)")
  }
}

func readFromKeychain(itemKey: String) {
  let queryLoad: [String: AnyObject] = [
    kSecClass as String: kSecClassGenericPassword,
    kSecAttrAccount as String: itemKey as AnyObject,
    kSecReturnData as String: kCFBooleanTrue,
    kSecMatchLimit as String: kSecMatchLimitOne,
  ]

  var result: AnyObject?

  let resultCodeLoad = withUnsafeMutablePointer(to: &result) {
    SecItemCopyMatching(queryLoad as CFDictionary, UnsafeMutablePointer($0))
  }

  if resultCodeLoad == noErr {
    if let result = result as? Data,
      let keyValue = NSString(data: result,
                             encoding: String.Encoding.utf8.rawValue) as? String {

      // Found successfully
      print(keyValue)
    }
  } else {
    print("Error loading from Keychain: \(resultCodeLoad)")
  }
}
like image 655
Evgenii Avatar asked Oct 29 '19 21:10

Evgenii


1 Answers

I enabled the keychain sharing from signing and capabilities section in xcode, and now I am able to store values in keychain.

like image 161
pravin Avatar answered Sep 29 '22 00:09

pravin