title: Accesing secure item in Keychain sometimes returns error -25308 (errSecInteractionNotAllowed) in iOS
body: <p>I'm finding that sometimes I get that error when trying to get a secure item I previously succesfully stored by using <code>Keychain</code> API. I have found it when the app running in my device was in background state and I locked the screen. The device hasn't lock code set, and this is the function I'm calling:</p>
  
  <pre><code>+ (NSString *)findValueForKey:(NSString *)keyStr
  {
     NSString *valueStr = @"";
  
     if ((keyStr != nil) &amp;&amp; (![keyStr isEqualToString:@""])) {
         NSString *service = [[NSBundle mainBundle] bundleIdentifier];
  
         NSDictionary *query = @{(__bridge id)kSecClass : (__bridge id)kSecClassGenericPassword,
                              (__bridge id)kSecAttrService : service,
                              (__bridge id)kSecAttrAccount : keyStr,
                              (__bridge id)kSecReturnData : (__bridge id)kCFBooleanTrue};
  
         CFDataRef cfValue = NULL;
         OSStatus results = SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef *)&amp;cfValue);
  
         if ([self checkIfNoError:results]) {
             valueStr = [[NSString alloc] initWithData:(__bridge_transfer NSData *)cfValue encoding:NSUTF8StringEncoding];
         }
  
         else {
            NSLog(@"%@", [self getErrorMessageForStatus:results]);
         }
     }
  
     return valueStr;
  }
  </code></pre>
  
  <p>and the item was stored by calling this method:</p>
  
  <pre><code>+ (BOOL)storeInKeychainWithKey:(NSString *)keyStr withValueStr:(NSString *)valueStr
  {
     if ((keyStr != nil) &amp;&amp; (![keyStr isEqualToString:@""]) &amp;&amp;
         (valueStr != nil) &amp;&amp; (![valueStr isEqualToString:@""])) {
  
         NSData *valueData = [valueStr dataUsingEncoding:NSUTF8StringEncoding];
         NSString *service = [[NSBundle mainBundle] bundleIdentifier];
  
         NSDictionary *secItem = @{(__bridge id)kSecClass : (__bridge id)kSecClassGenericPassword,
                                (__bridge id)kSecAttrService : service,
                                (__bridge id)kSecAttrAccount : keyStr,
                                (__bridge id)kSecValueData : valueData};
  
         CFTypeRef result = NULL;
  
         OSStatus status = SecItemAdd((__bridge CFDictionaryRef)secItem, &amp;result);
  
         NSLog(@"%@", [self getErrorMessageForStatus:status]);
  
        return [self checkIfItem:status];
     }
     else {
        return NO;
     }
  }
  </code></pre>
  
  <p>I thought that keychain items were always accessible in iOS... this <a href="https://stackoverflow.com/questions/9723949/ios-sfhfkeychainutils-failing-sometimes-with-error-25308-errsecinteractionnot">post</a> seems to be about something similar but I'm not sure if it is is deprecated and how should I solve this...</p>
  
  <p>Thanks in advance</p>