I am using a Swagger generated Swift Client which uses NSURLCredentials
to authenticate with a server. I am using persistence type .Permanent
My app communicates with the server in the background and works fine, except when I lock the phone (I see 401 errors in the log). I believe that NSURLCredential
is not able to access the keychain when the phone is locked.
I came across some posts which talk about enabling keychain access using kSecAttrAccessibleAfterFirstUnlock
. I haven't been able to figure out how to do this with NSURLCredential
. Does anyone know if and how this can be configured?
Asked
Active
Viewed 136 times
0

Ashish Tiwari
- 2,168
- 4
- 30
- 54

Dylan Colaco
- 325
- 2
- 11
1 Answers
0
Yes, that's exactly what is happening, in all likelihood. You have a couple of options:
- Delete the underlying keychain item and recreate it, specifying that it should be accessible when the device is unlocked. You don't need to use
NSURLCredential
at all with that approach. Just create an internet password item using Keychain Services APIs and the credential store should pick it up automatically. - Keep the credential data in memory, and implement the
URLSession:task:didReceiveChallenge:completionHandler:
delegate method in your session delegate (or the equivalent method for NSURLConnection if you're using that API) to provide the credential when asked.

dgatwood
- 10,129
- 1
- 28
- 49