iOS how to properly retrieve a credential from NSURLCredentialStorage?

303 views Asked by At

I'm running into an issue where I can't retrieve a credential from NSURLCredentialStorage, even if I try this immediately after saving.

The credential is properly formatted and is accepted by the server, it is set to be permanent. I have debug code to print and confirm that the credential has identity and certificate. However, when I attempt to retrieve it from credential storage, the debug prints all values as Null, while the credential itself is not null.

How do I properly retrieve credentials from NSURLCredentialStorage?

-(void)saveCredential:(NSURLCredential*)credential forProtectionSpace:(NSURLProtectionSpace*)protectionSpace
{

    CLog(@"ATTEMPTING SAVE:");
    [self debugCredential:credential]; //credential is properly formatted

    [[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential
                                          forProtectionSpace:protectionSpace];


    NSURLCredential* retrievedTest = [[NSURLCredentialStorage sharedCredentialStorage]defaultCredentialForProtectionSpace:protectionSpace];

    CLog(@"ATTEMPTING RETRIEVAL:");
    [self debugCredential:retrievedTest]; //all values are null, credential is not null

}

-(void)debugCredential:(NSURLCredential*) credential
{

    CLog(@"DEBUG Credential: [%@]",credential);
    CLog(@"credential identity: %@", credential.identity);
    CLog(@"credential cert: %@", credential.certificates);
    CLog(@"credential persistence: %lu", (unsigned long)credential.persistence);
}

Here are the logs before and after save

//before save
2016-02-17 16:24:19.669 GWNMobile[2925:716139] DEBUG Credential: [<NSURLCredential: 0x16ff6c00>: (null)]
2016-02-17 16:24:19.670 GWNMobile[2925:716139] credential identity: <SecIdentityRef: 0x16e24890>
2016-02-17 16:24:19.670 GWNMobile[2925:716139] credential cert: (
    "<cert(0x16f9c750) s: [Name] i: [Issuer]>"
)
2016-02-17 16:24:19.670 GWNMobile[2925:716139] credential persistence: 2

//after retrieval:
2016-02-17 16:24:19.670 GWNMobile[2925:716139] ==========================
2016-02-17 16:24:19.691 GWNMobile[2925:716139] ATTEMPTING RETRIEVAL:
2016-02-17 16:24:19.691 GWNMobile[2925:716139] DEBUG Credential: [(null)]
2016-02-17 16:24:19.691 GWNMobile[2925:716139] credential identity: (null)
2016-02-17 16:24:19.691 GWNMobile[2925:716139] credential cert: (null)
2016-02-17 16:24:19.691 GWNMobile[2925:716139] credential persistence: 0
0

There are 0 answers