我尝试编写rhodes本机扩展扩展来打开 iOS MailComposer 。 现在代码“工作”,但 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。如果需要,欢迎黑客。
Iosmailto的更新初始化:
- (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 {
}
答案 0 :(得分:5)
使用此代码呈现视图控制器
[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:composer animated:YES completion:nil];
答案 1 :(得分:1)
好的我认为问题是你的UIViewController在它的view属性中没有任何东西,因为你没有使用.xib文件来初始化它。 See this question以编程方式创建UIView并将其分配给UIViewController的view属性。我认为这是正确的方法。查看他们的-(void)loadView
方法的实现。
您的用例也有点奇怪,因为看起来您的Iosmailto UIViewController没有任何内容,并且您只是将它用作MFMailComposeViewController的包装器 - 您可以考虑直接重新实现您的C方法创建一个MFMailComposeViewController,没有不必要的UIViewController的间接层,这个UIViewController本身不会显示任何内容。