MFMailComposeViewController in a separate class

I am trying to create a "Utility Email Sender Class" that I can use in several iPhone projects.

I created the header and implementation of MailSender for this purpose.

MailSender.h:

@interface MailSender : NSObject<MFMailComposeViewControllerDelegate>

- (id) initWithParent:(UIViewController*) mainController;

- (void) invokeMailSender:(NSString*) to:(NSString*) subject:(NSString*) failureTitle:(NSString*) failureMessage:(NSString*) failureCancel;

@end

MailSender.m:

#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

I called the class from the view controller (in a button click action) using the following instructions:

@implementation InfoViewController

MailSender *sender;

- (IBAction)openMail:(id)sender
{
    sender = [[MailSender alloc] initWithParent:self];
    [sender invokeMailSender:@"test@test.com" :@"123" :@"123" :@"123" :@"123"];
}

.... 
@end

When I run the code, I can display it correctly. However, this is followed by collapse. Note that I have no crash when using MFMailComposeViewController directly from my UIViewController (and assigning the view manager as a delegate),

Any ideas? Sorry, I'm still new to Objective-C :)

+5
source share
1 answer

MailSender . invoke.

, property sender. .

@property (strong, nonatomic) MailSender *sender;
...
@synthesize sender = _sender;
...
self.sender = [[MailSender alloc] initWithParent:self];
[self.sender invokeMailSender:@"noor@dimachk.com" :@"123" :@"123" :@"123" :@"123"];

, . . .

- (void)invokeMailSender:(NSString *)sender 
                      to:(NSString *)to 
                 subject:(NSString *)subject 
            failureTitle:(NSString *)failureTitle 
          failureMessage:(NSString *)failureMessage 
failureCancelButtonTitle:(NSString *)failureCancelButtonTitle
+7

All Articles