0

我试图为打开iOS MailComposer编写 rhodes 本机扩展扩展。现在代码“工作”,但MFMailComposeViewController不显示,xcode 显示警告:

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

我读到 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

来自 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

在 iosmailto.m 中定义的 C 函数从 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];

此应用程序不适用于 AppStore。如果需要,欢迎使用 Hacks。

更新Iosmailto 的 init:

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

更新 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 {
}
4

2 回答 2

5

使用此代码呈现视图控制器

[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:composer
                                                                                                     animated:YES
                                                                                                   completion:nil];
于 2013-06-21T05:20:15.777 回答
1

好的,我认为问题在于您的 UIViewController 在其视图属性中没有任何内容,因为您没有使用 .xib 文件对其进行初始化。请参阅此问题,它以编程方式创建 UIView 并将其分配给 UIViewController 的 view 属性。我认为这是正确的方法。查看他们-(void)loadView方法的实现。

您的用例也有点奇怪,因为看起来您的 Iosmailto UIViewController 没有任何内容,而您只是将其用作 MFMailComposeViewController 的包装器——您可以考虑重新实现您的 C 方法以直接创建 MFMailComposeViewController没有这个不必要的 UIViewController 的不必要的间接层,它本身不显示任何东西。

于 2013-06-20T21:54:00.053 回答