我试图为打开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 {
}