title: AFNetworking authentication with web service
body: <p>Having an issue which is more of a design consideration than that of code. </p>
  
  <p>My iOS app interfaces with a json web service. I am using AFNetworking and my issue is basically I need the init function (which authenticates the AFHTTPClient and retrieves a token) to complete entirely before I make any additional requests (that require said token). </p>
  
  <p>From the code below, I would be interested in hearing design approaches to achieving this, I would prefer to keep all requests async an alternative solution would be to make the request in initWithHost:port:user:pass synchronous (not using AFNetworking) which I am aware is bad practice and want to avoid.</p>
  
  <p>DCWebServiceManager.h</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  #import "AFHTTPClient.h"
  
  @interface DCWebServiceManager : NSObject
  {
      NSString *hostServer;
      NSString *hostPort;
      NSString *hostUser;
      NSString *hostPass;
      NSString *hostToken;
      AFHTTPClient *httpClient;
  }
  
  // Designated Initialiser
  - (id)initWithHost:(NSString *)host port:(NSString *)port user:(NSString *)user pass:(NSString *)pass;
  
  // Instance Methods
  - (void)getFileList;
  @end
  </code></pre>
  
  <p>DCWebServiceManager.m</p>
  
  <pre><code>#import "DCWebServiceManager.h"
  #import "AFHTTPClient.h"
  #import "AFHTTPRequestOperation.h"
  #import "AFJSONRequestOperation.h"
  
  @implementation DCWebServiceManager
  
  - (id)initWithHost:(NSString *)host port:(NSString *)port user:(NSString *)user pass:(NSString *)pass
  {
      self = [super init];
      if (self)
      {
          hostServer = host;
          hostPort = port;
          hostUser = user;
          hostPass = pass;
  
          NSString *apiPath = [NSString stringWithFormat:@"http://%@:%@/", hostServer, hostPort];
  
          httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:apiPath]];
          [httpClient setAuthorizationHeaderWithUsername:hostUser password:hostPass];
  
          NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" path:@"authenticate.php" parameters:nil];
          AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
  
          [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
  
          // Do operations to parse request token to be used in
          // all requests going forward...
          // ...
          // ...
          // Results in setting: hostToken = '&lt;PARSED_TOKEN&gt;'        
              NSLog(@"HostToken: &gt;&gt;%@&lt;&lt;",  hostToken);
  
          } failure:^(AFHTTPRequestOperation *operation, NSError *error){
              NSLog(@"ERROR: %@",  operation.responseString);
          }];
  
          [operation start];
      }
  
      return self;
  }
  
  - (void)getFileList
  {
      // *************************
      // The issue is here, getFileList gets called before the hostToken is retrieved..
      // Make the authenticate request in initWithHost:port:user:pass a synchronous request perhaps??
      // *************************
      NSLog(@"IN GETFILELIST: %@", hostToken); // Results in host token being nil!!!
  
      NSString *queryString = [NSString stringWithFormat:@"?list&amp;token=%s", hostToken];
      NSMutableURLRequest *listRequest = [httpClient requestWithMethod:@"GET" path:queryString parameters:nil];
  
      AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:listRequest success:^(NSURLRequest *request,    NSHTTPURLResponse *response, id JSON){
          NSLog(@"SUCCESS!");
      } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
          NSLog(@"ERROR!: %@", error);
      }];
  
      [operation start];
  }
  @end
  </code></pre>
  
  <p>ViewController.m</p>
  
  <pre><code>....
  DCWebServiceManager *manager = [[DCWebServiceManager alloc] initWithHost:@"localhost" port:@"23312" user:@"FOO" pass:@"BAR"];
  [manager getFileList];
  
  // OUTPUTS
  IN GETFILELIST: (nil)
  HostToken: &gt;&gt;sdf5fdsfs46a6cawca6&lt;&lt;
  ....
  ...
  </code></pre>