title: Keychain items nil upon successful login
body: <p>I'm trying to upgrade my login system to include iOS Keychain. However, upon successful login, my keychain items are both nil. Currently, the code to save them is inside the same function called upon the click of the login button. Should I place them inside of the if statement itself? Should it have it's own function entirely?</p>
  
  <pre><code>- (IBAction)loginClicked:(id)sender {
      NSInteger success = 0;
      @try {
  
          KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"loginCredentials" accessGroup:nil];
          [keychainItem setObject:saveUsername forKey:(__bridge id)(kSecAttrAccount)];
          [keychainItem setObject:savePassword forKey:(__bridge id)(kSecValueData)];
  
  
          if([[self.usernameText text] isEqualToString:@""] || [[self.passwordText text] isEqualToString:@""] ) {
  
              [self alertStatus:@"Please enter Email and Password" :@"Sign in Failed!" :0];
  
          } else {
              NSString *post =[[NSString alloc] initWithFormat:@"username=%@&amp;password=%@",[self.usernameText text],[self.passwordText text]];
              NSLog(@"PostData: %@",post);
              NSURL *url=[NSURL URLWithString:@"http://www.techinworship.com/auth.php"];
              NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
              NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
              NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
              [request setURL:url];
              [request setHTTPMethod:@"POST"];
              [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
              [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
              [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
              [request setHTTPBody:postData];
  
              //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
  
              NSError *error = [[NSError alloc] init];
              NSHTTPURLResponse *response = nil;
              NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&amp;response error:&amp;error];
  
              NSLog(@"Response code: %ld", (long)[response statusCode]);
  
              if ([response statusCode] &gt;= 200 &amp;&amp; [response statusCode] &lt; 300)
              {
                  NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
                  NSLog(@"Response ==&gt; %@", responseData);
  
                  NSError *error = nil;
                  NSDictionary *jsonData = [NSJSONSerialization
                                            JSONObjectWithData:urlData
                                            options:NSJSONReadingMutableContainers
                                            error:&amp;error];
  
                  success = [jsonData[@"success"] integerValue];
                  NSLog(@"Success: %ld",(long)success);
  
                  if(success == 1)
                  {
                      NSLog(@"Login SUCCESS");
                      [self performSegueWithIdentifier:@"login_success" sender:self];
  
                  }
  
                  else {
  
                      NSString *error_msg = (NSString *) jsonData[@"error_message"];
                      [self alertStatus:error_msg :@"Sign in Failed!" :0];
                  }
  
              } else {
                  //if (error) NSLog(@"Error: %@", error);
                  [self alertStatus:@"Connection Failed" :@"Sign in Failed!" :0];
              }
          }
      }
      @catch (NSException * e) {
          NSLog(@"Exception: %@", e);
          [self alertStatus:@"Sign in Failed." :@"Error!" :0];
      }
  
  }
  </code></pre>