I am trying to implement keychain sharing using KeychainAccess. I have my main app that is suppossed to write a string to a shared keychain and a message filter extension where I want to read the data from the keychain and do some additional computations.
I declare everything in a struct as follows:
protocol Keychainable {
func save(string: String)
func get() -> String?
}
struct KeychainAccessStruct: Keychainable {
let keychain = Keychain(service: "app.test", accessGroup: "xxxxx.xxxxx.xxxxx.iFilterKeychain")
func save(string: String) {
let userData = try! NSKeyedArchiver.archivedData(withRootObject: string, requiringSecureCoding: false)
do {
try keychain.set(userData, key: "keychain_key")
} catch {
print(error)
}
}
func get() -> String? {
do {
let value = try keychain.getData("keychain_key")
return try! NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(value!) as! String
} catch {
print(error)
}
return nil
}
}
In the main app, when I try to save a string, I call:
let keychain = KeychainAccessStruct()
keychain.save(string: "STRING TO SAVE")
Then, in the app extension, when I try to read the string, I call:
let keychain = KeychainAccessStruct()
print(keychain.get())
The problem is that, when I run the extension, I receive the following error when trying to read from the keychain:
OSStatus error:[-25291] No keychain is available. You may need to restart your computer.
Also, if I try to retrieve the string from the keychain in the main app it's working fine, it looks like the problem only happens in the message filtering extension. I have looked over this question and it looks like I have all the setup just fine (I have included the app ID prefix in the method instantiation and the entitlements are included in both the app and the extension).
I have also seen this question and, while I have enabled the keychain sharing in capabilities, I seem to not be able to find what RequestsOpenAccess
refers to (although it's an older answer so maybe some things have changed?).
I am fairly new to swift and xcode in general so I am not certain what exactly I am missing. Can anyone help or point me in the right direction?