title: MFMailComposeViewController in a separate class
body: <p>I seek to create a "Utility Email sender class" that I can use in several iPhone projects.</p>
  
  <p>I created MailSender header and implementation for that purpose.</p>
  
  <p>MailSender.h:</p>
  
  <pre><code>@interface MailSender : NSObject&lt;MFMailComposeViewControllerDelegate&gt;
  
  - (id) initWithParent:(UIViewController*) mainController;
  
  - (void) invokeMailSender:(NSString*) to:(NSString*) subject:(NSString*) failureTitle:(NSString*) failureMessage:(NSString*) failureCancel;
  
  @end
  </code></pre>
  
  <p>MailSender.m:</p>
  
  <pre><code>#import "MailSender.h"
  
  @implementation MailSender
  
  MFMailComposeViewController* mailer;
  UIViewController* mailParentController;
  
  - (id) initWithParent:(UIViewController*) mainController
  {
      if( self = [super init])
      {
        mailParentController = mainController;
      }
      return self;
  }
  
  - (void) invokeMailSender:(NSString*) to:(NSString*) subject:(NSString*) failureTitle:(NSString*) failureMessage:(NSString*) failureCancel;
  
  {
      if([MFMailComposeViewController canSendMail])
      {
          mailer = [[MFMailComposeViewController alloc] init];
  
          mailer.mailComposeDelegate = self;
  
          [mailer setSubject:subject];
  
          NSArray *toRecipients = [NSArray arrayWithObjects:to, nil];
  
          [mailer setToRecipients:toRecipients];
         [mailParentController presentModalViewController:mailer animated:YES];
      }
      else
      {
          UIAlertView* alert = [[UIAlertView alloc] initWithTitle:failureTitle message:failureMessage
                                                         delegate:nil cancelButtonTitle:failureCancel otherButtonTitles: nil];
  
          [alert show];
      }
  }
  
  -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
  {
      // Do nothing
      [mailParentController dismissModalViewControllerAnimated:YES];
      mailer = nil;
  }
  
  
  
  @end
  </code></pre>
  
  <p>I called the class from a View Controller (in a button touch down action) using the following instructions:</p>
  
  <pre><code>@implementation InfoViewController
  
  MailSender *sender;
  
  - (IBAction)openMail:(id)sender
  {
      sender = [[MailSender alloc] initWithParent:self];
      [sender invokeMailSender:@"test@test.com" :@"123" :@"123" :@"123" :@"123"];
  }
  
  .... 
  @end
  </code></pre>
  
  <p>When I run the code, I am able to show the email views correctly. However, this is then followed by a crash.
  Note that I do not have a crash when using MFMailComposeViewController directly from my UIViewController (And assigning the View Controller as the delegate),</p>
  
  <p>Any ideas?
  Sorry I am still a new to Objective C :)</p>