I have this method that checks if we have a username and password stored in the keychain:
- (BOOL)hasLoginDetails
{
FLog
NSString *username = [self.keychainItem objectForKey:(__bridge id)kSecAttrAccount];
if (username == nil || [username isEqualToString:@""])
return NO;
NSData *passData = [self.keychainItem objectForKey:(__bridge id)kSecValueData];
if (passData == nil || [passData length] == 0)
return NO;
NSString *password = [[NSString alloc] initWithData:passData encoding:NSUTF8StringEncoding];
if (password == nil || [password isEqualToString:@""])
return NO;
return YES;
}
password is returned as NSData
so needs converting to a string. This works perfectly fine in normal use however when it's used in the background application:performFetchWithCompletionHandler:
method I sometimes get crashes pointing to this line:
NSString *password = [[NSString alloc] initWithData:passData encoding:NSUTF8StringEncoding];
the crash log complains of
-[__NSCFString bytes]: unrecognized selector sent to instance 0x156104a0
I only see these crashes after distributing AdHoc builds.
I'm assuming the crash log is saying that the passData
variable is actually a string at this point?
Any ideas what's going on here?
Thanks
Edit ----
The password is added to the keychain simply using [self.keychainItem setObject:password forKey:(__bridge id)kSecValueData];
password
being an NSString. I'm using KeychainItemWrapper from here which converts the password NSString to NSData but doesn't convert it back again which is why I am doing it.
Edit 2 ----
After a debug crash, I check thee passData variable and it was in fact the NSString that should be NSData, so sometimes I'm getting NSData and every now and then i'm getting NSString.