title: Block not completing in iOS
body: <p>I'm relatively new to iOS development but I'm working on an application to get a better understanding of development. I'm working with a web service and want to check the credentials a user enters. To do this I am making a simple get request with their credentials and then checking the http status for 200. Here is my code below:</p>
  
  <pre><code>-(BOOL)checkCredentials:(NSString *)username withPassword:(NSString *)password{
  
      NSString *requestString = @"SOME URL";
      NSURL *url = [NSURL URLWithString:requestString];
      NSURLRequest *req = [NSURLRequest requestWithURL:url];
  
      NSData *userPasswordData = [[NSString stringWithFormat:@"%@:%@", username, password] dataUsingEncoding:NSUTF8StringEncoding];
      NSString *base64EncodedCredential = [userPasswordData base64EncodedStringWithOptions:0];
      NSString *authString = [NSString stringWithFormat:@"Basic %@", base64EncodedCredential];
  
      NSURLSessionConfiguration *sessionConfig=[NSURLSessionConfiguration defaultSessionConfiguration];
      sessionConfig.HTTPAdditionalHeaders=@{@"Authorization":authString};
  
      self.session=[NSURLSession sessionWithConfiguration:sessionConfig];
      __block BOOL success = NO;
  
      dispatch_semaphore_t sema = dispatch_semaphore_create(0);
  
      NSURLSessionDataTask *dataTask = [self.session dataTaskWithRequest:req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  
          if(!error){
              NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
              if (httpResp.statusCode == 200) {
                  success = YES;
              }
          }
          NSMutableDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
          NSLog(@"%@", jsonObject);
          dispatch_semaphore_signal(sema);
      }];
  
      dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
  
      [dataTask resume];
      return success;
  }
  </code></pre>
  
  <p>I was going to use a semaphore to wait for the block to complete so I can check the status code and then return. But first it seems like my code just hangs, and I think that because I don't have a release, but that's not allowed with ARC. I'm not sure why it's hanging. Is there a better way to wait for the block to complete (without a semaphore) so I can return whether my credentials are valid? </p>
  
  <p>Also is there a better way to pass the username and password so that it's not possible for someone to spoof the username and password?</p>
  
  <p>Any help would be greatly appreciated.  </p>