title: iOS - in a view controller, where is the most logical place to put user-session management code?
body: <p>I am an iOS newbie and I am trying to understand some of the "good practice" ways of doing things in an iOS app.</p>
  
  <p>I have a ViewController which I made to be the controller that is called when the app starts.  In it I saw a function called viewDidLoad and I tried to modify it to check if the user has had a user_id and then call a function that would eventually make an asynchronous request to manage that user in a remote db.  Here is my code:</p>
  
  <pre><code>- (void)viewDidLoad
  {
      [super viewDidLoad];
  
      EmailUtil *email = [EmailUtil alloc];
      email = [email init];
  
      // This is just a test call to the function that would make a remote server request
      [email setEmail: @"test" andBody: @"hello"];
  
  
      NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
      if([standardUserDefaults objectForKey:@"user_id"] == nil)
      {
          NSLog(@"First time"); 
  
          [standardUserDefaults setBool:YES forKey:@"user_id"];      
  
      }
      else 
      {
          NSString *subject = @"subject";
          NSString *body = @"bod";
  
           NSLog(@"Not first time"); 
      }    
  }
  </code></pre>
  
  <p>So there are a few things I am unsure with here. Can the fact that I am calling a function to make a remote call from viewDidLoad cause problems in that function? Currently its not sending the remote request.</p>
  
  <p>Also, should I deallocate memory for the objects I create right at the end of this function? </p>
  
  <p>Or should I just move this code to another spot in the class where it would make more sense to have that code there?</p>
  
  <p>I call the email object like this:</p>
  
  <pre><code>[email setEmail: @"test" andBody: @"hello"];
  </code></pre>
  
  <p>and here is the code for the EmailUtil class:</p>
  
  <pre><code>//
  //  EmailUtil.m
  //
  
  #import "EmailUtil.h"
  
  @implementation EmailUtil
  
  -(void) setEmail: (NSString *) subject andBody: (NSString *) body
  {   
      NSString *final_url = [NSString stringWithFormat:@"http://www.my_url.com?subject=%@&amp;body=%@",subject, body];
      NSURL *url = [NSURL URLWithString:final_url];
  
      NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url ];
  
      // TODO: ok I dont really understand what this is
      NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  
      [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
       {         
           NSLog(@"On return");
  
           NSLog(@"This is data: %@" , data);
           NSLog(@"This is response: %@" , response);
           NSLog(@"This is error: %@" , error);
  
           NSLog(@"OK");
  
       }];    
  
  
  }
  @end
  </code></pre>
  
  <p>Thank you!</p>