SwiftKeychainWrapper icon indicating copy to clipboard operation
SwiftKeychainWrapper copied to clipboard

KeychainWrapper.standard.set always returning false

Open ehalai opened this issue 8 years ago • 18 comments

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.

ehalai avatar Nov 22 '16 09:11 ehalai

I'm experiencing the same as yours buddy.

cHaLkdusT avatar Nov 22 '16 11:11 cHaLkdusT

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.

jrendel avatar Nov 22 '16 13:11 jrendel

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.

thomascsorbamedia avatar Dec 27 '16 17:12 thomascsorbamedia

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.

jrendel avatar Dec 31 '16 16:12 jrendel

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.

Montrazul avatar Mar 06 '17 12:03 Montrazul

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

OlesenkoViktor avatar Apr 18 '17 20:04 OlesenkoViktor

Facing this problem as well. KeychainWrapper.standard works fine

ppakorn avatar Apr 27 '17 06:04 ppakorn

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;

artemtkachenko avatar Jun 26 '17 09:06 artemtkachenko

anything to fix this?

StevenArmandLee avatar Aug 16 '17 07:08 StevenArmandLee

I'm facing same issue with Swift 4 and Xcode 9.0. Using a singleton works for me though.

kmkrn avatar Oct 25 '17 10:10 kmkrn

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.

yangxinliang avatar Jun 07 '18 02:06 yangxinliang

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... :(

silvaric avatar Sep 26 '18 14:09 silvaric

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.

  1. Turn on Keychain sharing screen shot 2018-09-27 at 12 45 54 pm
  2. Specify Keychain group name image
  3. Append your App ID screen shot 2018-09-27 at 1 04 12 pm
  4. Now your code snippet should look like this screen shot 2018-09-27 at 1 07 01 pm

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 avatar Sep 27 '18 05:09 cHaLkdusT

@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...

NunoAlexandre avatar Oct 11 '18 09:10 NunoAlexandre

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 avatar Oct 11 '18 10:10 cHaLkdusT

@cHaLkdusT thanks. That's what I am doing, I was concerned it would work locally and fail in production.

NunoAlexandre avatar Oct 11 '18 11:10 NunoAlexandre

@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. calendykeyboard_xcodeproj

kazuooooo avatar Dec 30 '18 05:12 kazuooooo

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.

  1. Turn on Keychain sharing
screen shot 2018-09-27 at 12 45 54 pm
  1. Specify Keychain group name image

  2. Append your App ID screen shot 2018-09-27 at 1 04 12 pm

  3. Now your code snippet should look like this screen shot 2018-09-27 at 1 07 01 pm

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.

tejas786u avatar Jul 24 '20 06:07 tejas786u