Search code examples
iphoneiosuiviewcontrollermfmailcomposeviewcontrollerrhomobile

Ios MFMailComposeViewController not displaing


I trying to write rhodes native extension extension for opening iOS MailComposer. Now code "works", but MFMailComposeViewController not displaying and xcode shows warning:

Attempt to present <MFMailComposeViewController: 0x12b60840> on <Iosmailto: 0xc1898d0> whose view is not in the window hierarchy!

I read that UIViewControllers must be in hierarchy, but i can't provide this from Rhodes ruby code or clear-C extension wrapper. Now i'm trying to present my UIViewController with MFMailComposeController from [UIApplication sharedApplication].keyWindow.rootViewController but mail composer not show yet.

interface from iosmailto.h:

@interface Iosmailto : UIViewController<MFMailComposeViewControllerDelegate>
{
}
    @property(nonatomic, assign) id delegate;
    //-(IBAction)send_mail:(id)sender;
@end

implementation from iosmailto.m:

@implementation Iosmailto

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self send_mail];
    }

    - (void)send_mail {
        if ([MFMailComposeViewController canSendMail]) {

            MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
            composer.mailComposeDelegate = self;
            [composer setSubject:[NSString stringWithUTF8String:subj]];

            NSArray *recipients_array = [NSArray arrayWithObject:[NSString stringWithUTF8String:recipients]];

            [composer setToRecipients:recipients_array];

            NSString *composerBody = [NSString stringWithUTF8String:message];
            [composer setMessageBody:composerBody isHTML:NO];

            [composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
            [self presentModalViewController:composer animated:YES];

            [composer release];
        } else {

            NSLog(@"Device is unable to send email in its current state.");
        }
    }
    /* some unnecessary for this question methods */

@end

And C function defined in iosmailto.m thats called from Rhodes:

// find root vc
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

// find topmost vc
while (topController.presentedViewController) {
    topController = topController.presentedViewController;
}

iosmailer = [[Iosmailto alloc] init];

iosmailer.delegate = topController;
[topController presentViewController:iosmailer animated:YES completion:nil];

This app is not for AppStore. Hacks is welcome if needed.

Update init for Iosmailto:

- (id)init
{
   self = [super initWithNibName:nil bundle:nil];
   return self;
}

Update 2 The short version of this code (without UIViewController wrapper) was my starting point. That's don't work too. And this have the same warning and one more:

UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

while (topController.presentedViewController) {
    topController = topController.presentedViewController;
}
if ([MFMailComposeViewController canSendMail]) {
    MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
    composer.mailComposeDelegate = (id<MFMailComposeViewControllerDelegate>)topController ;

    [composer setSubject:[NSString stringWithUTF8String:subj]];
    NSArray *recipients_array = [NSArray arrayWithObject:[NSString stringWithUTF8String:recipients]];
    [composer setToRecipients:recipients_array];
    NSString *composerBody = [NSString stringWithUTF8String:message];
    [composer setMessageBody:composerBody isHTML:NO];
    [composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [topController presentModalViewController:composer animated:YES];

    [composer release];
} else {
}

Solution

  • Use this code to present view controller

    [[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:composer
                                                                                                         animated:YES
                                                                                                       completion:nil];