title: Prevent Man in the Middle attack with AFNetworking
body: <p>I am using AFNetworking to send data over SSL. How do I prevent man-in-the-middle attacks? How can I be sure that I am receiving and sending the data to my server and not some other server?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>    @implementation API
  
  @synthesize user;
  
  #pragma mark - Singleton methods
  /**
   * Singleton methods
   */
  +(API*)sharedInstance
  {
      static API *sharedInstance = nil;
      static dispatch_once_t oncePredicate;
      dispatch_once(&amp;oncePredicate, ^{
          sharedInstance = [[self alloc] initWithBaseURL:[NSURL URLWithString:kAPIHost]];
      });
  
      return sharedInstance;
  }
  
  #pragma mark - init
  //intialize the API class with the destination host name
  
  -(API*)init
  {
      //call super init
      self = [super init];
  
      if (self != nil) {
          //initialize the object
          user = nil;
  
          [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
  
          [self setDefaultHeader:@"Accept" value:@"application/json"];
      }
  
      return self;
  }
  
  -(BOOL)isAuthorized
  {
      return [[user objectForKey:@"id"] intValue]&gt;0;
  }
  
  
  
  -(void)commandWithParams:(NSMutableDictionary*)params
                      path: (NSString *) pathstr
              onCompletion:(JSONResponseBlock)completionBlock
  {
      NSMutableURLRequest *apiRequest =
      [self multipartFormRequestWithMethod:@"POST"
                                      path: pathstr
                                parameters: params
                 constructingBodyWithBlock: ^(id &lt;AFMultipartFormData&gt;formData)
                 {
                     //TODO: attach file if needed
                 }];
  
      AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest: apiRequest];
      [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
          //success!
          completionBlock(responseObject);
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          //failure :(
  
          NSLog(@"%@", error);
          completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
      }];
  
      [operation start];
  
  }
  @end
  </code></pre>
  
  <p>and then I call:</p>
  
  <pre><code>[[API sharedInstance] commandWithParams: params
                                     path: @"user/registration"
                             onCompletion:^(NSDictionary *json)
   {
  </code></pre>