title: OSStatus error code -34018
body: <p>I am using <code>SecItemCopyMatching</code> to access the iOS keychain. About 1 in a hundred times I get a <code>-34018</code> result code right after relaunching the app from the background. <a href="https://developer.apple.com/library/ios/documentation/Security/Reference/keychainservices/#//apple_ref/doc/uid/TP30000898-CH5g-CJBEABHG" rel="noreferrer">The documentation</a> states:</p>
  
  <blockquote>
    <p>The assigned error space for Keychain Services is discontinuous:
    –25240 through –25279 and –25290 through –25329. Keychain Item
    Services may also return noErr (0) or paramErr (–50), or CSSM result
    codes</p>
  </blockquote>
  
  <p>So it seems that <code>-34018</code> is a 'CSSM result code'. I have followed the <a href="http://www.opengroup.org/security/cdsa.htm" rel="noreferrer">suggested link</a> but could not find result codes.</p>
  
  <p>What it the <code>-34018</code> result code? How can I get more reliable keychain access?</p>
  
  <pre><code>- (NSData *)getKeychainData:(NSString *)key
  {
      NSDictionary *query = @{
          (__bridge id)kSecClass:(__bridge id)kSecClassGenericPassword,
          (__bridge id)kSecAttrService:SEC_ATTR_SERVICE,
          (__bridge id)kSecAttrAccount:key,
          (__bridge id)kSecReturnData:@YES
      };
  
      CFDataRef result = nil;
  
      OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef *)&amp;result);
  
      if(status == errSecItemNotFound) {
          return nil;
      }
  
      if(status == noErr) {
          return CFBridgingRelease(result);
      } else {
          [self logError:[NSString stringWithFormat:@"SecItemCopyMatching status %d", (int)status] :nil];
          return nil;
      }
  }
  </code></pre>