SwiftKeychainWrapper
SwiftKeychainWrapper copied to clipboard
KeychainWrapper.standard.set always returning false
I have installed branch Swift2.3 via cocoa pods.
I am calling the following code:
let saveSuccessful: Bool = KeychainWrapper.standard.set("username123", forKey: "username")
But saveSuccesful
is always returning false. I have tried with different strings. I have also tried calling:
let retrievedString: String? = KeychainWrapper.standard.string(forKey: "username")
To double check it's not just the wrong value being returned but retrievedString
is always nil.
Am I doing something wrong? Any help would be appreciated.
I'm experiencing the same as yours buddy.
Are you guys seeing this on the iOS 10 simulator? If so its probably this issue: https://github.com/jrendel/SwiftKeychainWrapper/issues/59
That issue lists some work arounds, but I've also heard its fixed in Xcode 8.2.
I'm seeing this on the iOS 10 device. It always returns false. I'm using the latest Xcode, version 8.2.1 and iOS 10.2 In the simulator iOS 10.2 it works fine but in the simulator there are no biometrics set up in it.
So you are seeing this on a device, not on the simulator, and specifically with the Swift2.3 branch? I'll have to do some testing on that branch to see whats up.
I also faced this problem with SwiftKeychainWrapper Version 3.0.1
on iOS 10+ Devices BUT only if I used a custom keychain wrapper like the following:
static let UNIQUE_SERVICE_NAME = "someuniqueservicename";
static let UNIQUE_SERVICE_GROUP = "someuniqueservicegroup";
let customKeychain = KeychainWrapper(serviceName: UNIQUE_SERVICE_NAME, accessGroup: UNIQUE_SERVICE_GROUP);
func setValue(value: String) {
boolean success = self.customKeychain.set(value: forKey: "Key");
print("success: \(success)");
}
It would always return false and also accessing the value for the key "Key" would always return nil.
If I use the default KeychainWrapper
instead:
func setValue(value: String) {
boolean success = KeychainWrapper.standard.customKeychain.set(value: forKey: "Key");
print("success: \(success)");
}
Everything works fine.
Faced with this problem today with custom keychain:
- iOS 9.3 iPhone 4S simulator
- iOS 10.3.1 iPhone 5S device
private let keychain = KeychainWrapper(serviceName: Bundle.main.bundleIdentifier!, accessGroup: "com.group")
I couldn't use default keychain or UserDefaults, bcs need share private info to AppExtension
Facing this problem as well. KeychainWrapper.standard works fine
Same issue; iOS 10.3.2; xcode 8.3.3
after adding property: let status: OSStatus = SecItemAdd(keychainQueryDictionary as CFDictionary, nil)
status == -34018
KeychainWrapper.standard works as expected;
anything to fix this?
I'm facing same issue with Swift 4 and Xcode 9.0. Using a singleton works for me though.
let appServiceName = "customkey" let aqueAccessGroup = "customkey" let customKeychain = KeychainWrapper(serviceName: appServiceName, accessGroup: aqueAccessGroup);
let success: Bool = customKeychain.set(value:"abc" forKey: "Key");
print("success: \(success)");
It would always return false and also accessing the value for the key "Key" would always return nil.
but
let success: Bool = KeychainWrapper.standard.set(value:"abc" forKey: "Key");
Everything works fine.
Any update? Same issue for a custom keychain with Swift 4.1 and Xcode 9.4.1 @swiftthesorrow I tried with a singleton but it is the same... :(
Hi all,
I was able to figure out on how to fix on both simulator and iPhone. You need you enable sharing of Keychain between apps.
- Turn on Keychain sharing
- Specify Keychain group name
- Append your App ID
- Now your code snippet should look like this
NOTE: Without appending your App ID, it will return OSStatus error of -34018 | Internal error when a required entitlement isn't present.
Hope this helps.
Cheers,
@cHaLkdusT your tutorial is very clear, but I am confused about whether we need to enable this keychain sharing in order to store a secret on a device's/Apple ID keychain, without the need to share it across apps. I am able to keep a secret on my device's keychain that is persisted across (un)installs without this keychain sharing option on...
Heya @NunoAlexandre,
You could just use the generic:
let saveSuccessful: Bool = KeychainWrapper.standard.set("Some String", forKey: "myKey")
if you don't want to use access group.
Besides, you can only share Keychain between the apps that are signed with your account certificate
@cHaLkdusT thanks. That's what I am doing, I was concerned it would work locally and fail in production.
@cHaLkdusT Thank you for clear solution!
I also need to add Keychain Sharing setting in shared app to access from other app, otherwise return nil.
Hi all,
I was able to figure out on how to fix on both simulator and iPhone. You need you enable sharing of Keychain between apps.
- Turn on Keychain sharing
![]()
Specify Keychain group name
Append your App ID
Now your code snippet should look like this
NOTE: Without appending your App ID, it will return OSStatus error of -34018 | Internal error when a required entitlement isn't present.
Hope this helps.
Cheers,
Thanks a lot, This is perfectly working fine for me.