UICKeyChainStore icon indicating copy to clipboard operation
UICKeyChainStore copied to clipboard

Returns nil when just appeared from background

Open k06a opened this issue 11 years ago • 35 comments

This code sometimes returns nil when my app is just appeared from background:

[UICKeyChainStore dataForKey:self.privateKeyTag]

When I restart app - it returns non-nil value.

k06a avatar Oct 16 '14 15:10 k06a

I'm getting this same behavior.

ChristopherCarranza avatar Oct 30 '14 05:10 ChristopherCarranza

I think this may happens because of iPhone locking. It always auto-locks because of touch-ID enabled. So some keychain items can be unavailable IMHO.

k06a avatar Oct 30 '14 05:10 k06a

Could be, looks like its hitting line 153 and returning nil there. SecItemCopyMatching is not returning a success.

ChristopherCarranza avatar Oct 30 '14 05:10 ChristopherCarranza

Same error here. Log:

Communications error: <OS_xpc_error: <error: 0x19c8a3a80> { count = 1, contents =
    "XPCErrorDescription" => <string: 0x19c8a3e78> { length = 22, contents = "Connection interrupted" }
}>
Error Domain=com.kishikawakatsumi.uickeychainstore Code=-34018 "The operation couldn’t be completed. (com.kishikawakatsumi.uickeychainstore error -34018.)"

elado avatar Dec 27 '14 10:12 elado

I'm also having an issue with returning nil when coming back to app

rajohns08 avatar Jan 17 '15 05:01 rajohns08

I've solved problem by caching keychain data in property.

@property (nonatomic, strong) NSData *privateKey;
...
- (NSData *)privateKey {
    if (_privateKey == nil)
        _privateKey = [UICKeychainStore dataForKey:@"mykeytag"];
    return _privateKey;
}

k06a avatar Jan 17 '15 06:01 k06a

What's weird is, I saw this issue twice within 30 minutes of loading an app with UICKeyChainStore for the first time. The following 24 hours I never could reproduce it a single time.

rajohns08 avatar Jan 18 '15 17:01 rajohns08

Getting the same bug here. Here's how I consistently reproduce it:

  1. Hit home button to put app into background
  2. Open up 6-10 other apps to make sure iOS puts it fully in the background
  3. Open app and quickly call stringForKey:error:. Receives error "Error getting keychain value: Security error has occurred."

briankracoff avatar Jan 26 '15 16:01 briankracoff

please take a look to the UICKeychain documentation. and search for keywords "Configuration (Accessibility, Sharing, iCould Sync)"

you need to call the [keyChain setAccessibility: ....... ] function. Cheers :dancers:

pandaxbacon avatar Feb 06 '15 15:02 pandaxbacon

Same here randomly returning nil with warning "OSStatus error: [-34018] Security error has occurred"

For me this wasn't due to application going to background/foreground, it just happens randomly Setting accessibility to UICKeyChainStoreAccessibilityWhenUnlocked, and setting a specific service did not fix the issue either

aryaxt avatar Mar 12 '15 00:03 aryaxt

Having the same issue here, the application goes back from background, and I have the "Security error has occurred" both when trying to set and get a value from the keychain.

jayztemplier avatar Mar 17 '15 02:03 jayztemplier

Alright so I made it work on my device. All it took was to set nil to the accessGroup, and rely on the fact that it takes the first group in the entitlement as default group.

I don't know if it's a bug within iOS or a signing issue.

jayztemplier avatar Mar 17 '15 18:03 jayztemplier

@jayztemplier how did you set the accessGroup to nil? I am getting the same issue and curretnly it is a read only property. Did you mod the source?

theprojectabot avatar Mar 19 '15 20:03 theprojectabot

I can consistently reproduce this too, doing what @briankracoff described. @jayztemplier 's solution unfortunately did not work for me. Version 1 was working fine. Will investegate.

jondal avatar Apr 10 '15 17:04 jondal

@jayztemplier same thing

ilya-ramanenia avatar May 05 '15 18:05 ilya-ramanenia

anyone fix it?

dannyjiajia avatar May 19 '15 14:05 dannyjiajia

@dannyjiajia I just tried this because I had a thought last night:

in - (void)applicationDidBecomeActive:(UIApplication *)application { [UICKeyChainStore keyChainStoreWithService:NAME_OF_YOUR_SERVICE accessGroup:nil];

}

It seemed to have worked!

Try it out?

theprojectabot avatar May 19 '15 17:05 theprojectabot

@theprojectabot Your solution unfortunately did not work for me.I cached the value as @k06a

dannyjiajia avatar May 20 '15 01:05 dannyjiajia

I am also experiencing this problem already in :

-(void)applicationWillEnterForeground:(UIApplication *)application { UICKeyChainStore *keyChainStore = [UICKeyChainStore keyChainStoreWithService:SERVICE accessGroup:nil];

}

I am also getting the error: OSStatus error: [-34018] Security error has occurred. However, it looks like it only happens when I am debugging the build. Haven't seen it else - Anyone who can "confirm"?

Mortenest avatar Jun 18 '15 09:06 Mortenest

@MortenFalcon happens to me too only while debugging, and not always.

cristiankocza-sv avatar Jun 23 '15 06:06 cristiankocza-sv

+1 while debugging only

allenhsu avatar Jul 14 '15 07:07 allenhsu

Why is this happening? Is there any fix?

jakedunc avatar Jul 17 '15 14:07 jakedunc

Same issue here, only occurs sometimes, but its annoying because I use it to manage the state of the app.

gconno avatar Jul 22 '15 15:07 gconno

+1 while debugging it appears. Doesnt affect the app on the store at the moment.

gconno avatar Jul 24 '15 11:07 gconno

Is anybody solve this problem ?

zhouzhuhao avatar Aug 25 '15 08:08 zhouzhuhao

OSStatus error: [-34018] Security error has occurred. this problem occurs sometimes,can anybody help me?

zhouzhuhao avatar Aug 25 '15 08:08 zhouzhuhao

Just cache this value in property.

k06a avatar Aug 26 '15 12:08 k06a

Any solution?

shekbagg avatar Oct 26 '15 09:10 shekbagg

This is a known apple bug in the keychain that has yet to be fixed https://forums.developer.apple.com/thread/4743

Facebook and others that depend on app switching for oauth are notably affected. https://m.facebook.com/login.php?next=https%3A%2F%2Fdevelopers.facebook.com%2Fbugs%2F136880803313865&refsrc=https%3A%2F%2Fwww.facebook.com%2Flogin.php&_rdr

Blahartinger avatar Oct 26 '15 13:10 Blahartinger

@Blahartinger thanks for your helpful links.

dinhnhat0401 avatar Nov 24 '15 08:11 dinhnhat0401