jrendel / SwiftKeychainWrapper

A simple wrapper for the iOS Keychain to allow you to use it in a similar fashion to User Defaults. Written in Swift.
MIT License
1.59k stars 340 forks source link

KeychainWrapper.standard.set always returning false #78

Open ehalai opened 7 years ago

ehalai commented 7 years ago

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.

cHaLkdusT commented 7 years ago

I'm experiencing the same as yours buddy.

jrendel commented 7 years ago

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.

thomascsorbamedia commented 7 years ago

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.

jrendel commented 7 years ago

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.

Montrazul commented 7 years ago

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.

OlesenkoViktor commented 7 years ago

Faced with this problem today with custom keychain:

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

ppakorn commented 7 years ago

Facing this problem as well. KeychainWrapper.standard works fine

artemtkachenko commented 7 years ago

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;

StevenArmandLee commented 7 years ago

anything to fix this?

kmkrn commented 7 years ago

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

yangxinliang commented 6 years ago

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.

silvaric commented 6 years ago

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

cHaLkdusT commented 6 years ago

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,

NunoAlexandre commented 6 years ago

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

cHaLkdusT commented 6 years ago

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

NunoAlexandre commented 6 years ago

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

kazuooooo commented 5 years ago

@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
tejas786u commented 4 years ago

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.