我遇到了一个无法从NSURLCredentialStorage
检索凭据的问题,即使我在保存后立即尝试这样做。
凭证格式正确并且被服务器接受,它被设置为永久性的。我有调试代码打印并确认凭证有身份和证书。但是,当我尝试从凭据存储中检索它时,调试会将所有值打印为Null,而凭据本身不为null。
如何从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);
}
以下是保存
之前和之后的日志//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