title: SecItemAdd and SecItemCopyMatching returns error code -34018 (errSecMissingEntitlement)
body: <p>Sometimes when I run an application on device from Xcode I would try to access the keychain but fail due to error -34018. This doesn't match any of the documented keychain error codes and can't be consistently reproduced. (happens maybe 30% of the time, and it's not clear to me why it happens). What makes debugging this problem very difficult is the total lack of documentation. Any idea what causes this and how to fix it? I'm using Xcode 5 and running iOS 7.0.4 on device.</p>
  
  <p>There is an open issue about this here: <a href="https://github.com/soffes/sskeychain/issues/52">https://github.com/soffes/sskeychain/issues/52</a></p>
  
  <p><strong>EDIT: Adding keychain access code per request</strong></p>
  
  <p>I'm using the <code>SSKeychain</code> library for interfacing with keychain. Here's the snippet.</p>
  
  <pre><code>#define SERVICE @"default"
  
  @implementation SSKeychain (EXT)
  
  + (void)setValue:(NSString *)value forKey:(NSString *)key {
      NSError *error = nil;
      BOOL success = NO;
      if (value) {
          success = [self setPassword:value forService:SERVICE account:key error:&amp;error];
      } else {
          success = [self deletePasswordForService:SERVICE account:key error:&amp;error];
      }
      NSAssert(success, @"Unable to set keychain value %@ for key %@ error %@", value, key, error);
      if (!success) {
          LogError(@"Unable to set value to keychain %@", error);
      }
      LogTrace(@"Will set keychain account %@. is to nil? %d", key, value == nil);
      if (value == nil)
          LogWarn(@"Setting keychain %@ to nil!!!", key);
  }
  
  + (NSString *)valueForKey:(NSString *)key {
      NSError *error = nil;
      NSString *value = [self passwordForService:SERVICE account:key error:&amp;error];
      if (error &amp;&amp; error.code != errSecItemNotFound) {
          NSAssert(!error, @"Unable to retrieve keychain value for key %@ error %@", key, error);
          LogError(@"Unable to retrieve keychain value for key %@ error %@", key, error);
      }
      return value;
  }
  
  + (BOOL)removeAllValues {
      LogInfo(@"Completely Reseting Keychain");
      return [[self accountsForService:SERVICE] all:^BOOL(NSDictionary *accountInfo) {
          return [self deletePasswordForService:SERVICE account:accountInfo[@"acct"]];
      }];
  }
  
  @end
  </code></pre>
  
  <p>Vast majority of the time it's just fine. Sometimes I'll hit the assertion failures where I'm unable to either write to or read from keychain, causing critical assertion failure. </p>