Ios MFMailComposeViewController没有移位

roo*_*tar 0 iphone uiviewcontroller rhomobile ios mfmailcomposeviewcontroller

我试图编写rhodes本机扩展扩展来打开iOS MailComposer.现在代码"工作",但MFMailComposeViewController没有显示,xcode显示警告:

Attempt to present <MFMailComposeViewController: 0x12b60840> on <Iosmailto: 0xc1898d0> whose view is not in the window hierarchy!
Run Code Online (Sandbox Code Playgroud)

我读到UIViewControllers必须在层次结构中,但我不能从Rhodes ruby​​代码或clear-C扩展包装器中提供.现在我正在尝试使用来自[UIApplication sharedApplication] .keyWindow.rootViewController的MFMailComposeController呈现我的UIViewController,但邮件编辑器尚未显示.

来自iosmailto.h的界面:

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

从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
Run Code Online (Sandbox Code Playgroud)

和从Rhodes调用的iosmailto.m中定义的C函数:

// 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];
Run Code Online (Sandbox Code Playgroud)

此应用程序不适用于AppStore.如果需要,欢迎黑客入侵.

更新 Iosmailto的init:

- (id)init
{
   self = [super initWithNibName:nil bundle:nil];
   return self;
}
Run Code Online (Sandbox Code Playgroud)

更新2此代码的简短版本(没有UIViewController包装器)是我的起点.那也行不通.这有同样的警告,还有一个:

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 {
}
Run Code Online (Sandbox Code Playgroud)

Des*_*ose 5

使用此代码显示视图控制器

[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:composer
                                                                                                     animated:YES
                                                                                                   completion:nil];
Run Code Online (Sandbox Code Playgroud)