我想MFMailComposeViewController
从模态视图控制器中呈现一个。基本上这种方法有效,但它不能可靠地工作:
-(void)sendMailTapped:(id)sender{
[self resetButtonsStateAfterTapping:sender];
[self dismissPopover];
if (filesize>10) {
[self showAlertForExceededMaximumAttachmentSize];
return;
}
@try {
MFMailComposeViewController *picker =
[[MFMailComposeViewController alloc] init];
if ([MFMailComposeViewController canSendMail]) {
picker.mailComposeDelegate = self;
NSURL *path =[NSURL urlWithPath:[pageInfoDict valueForKey:@"file_name"]
docId:[pageInfoDict valueForKey:@"id_doc"]
encrypted:[[pageInfoDict valueForKey:@"encrypted"]
boolValue]] ;
NSString *fileName = [pageInfoDict valueForKey:@"title"];
if([fileName length] == 0) {
fileName = [path lastPathComponent];
}
if(![fileName hasSuffix:[path pathExtension]]){
fileName=[fileName stringByAppendingFormat:@".%@",[path pathExtension]];
}
[picker setSubject:[@"Send document: " stringByAppendingString:fileName]];
NSArray *ccRecipients = [NSArray arrayWithObjects:
[[CustomisationConfig getAppConfig] getCCMail],nil];
[picker setCcRecipients:ccRecipients];
NSArray *bccRecipients = [NSArray arrayWithObjects:
[[CustomisationConfig getAppConfig] getBCCMail],nil];
[picker setBccRecipients:bccRecipients];
NSData *myData = [path decryptedData];
[picker addAttachmentData:myData
mimeType:fileMIMEType(fileName) fileName:fileName];
NSString *emailBody = [NSString stringWithFormat:
@"\n\nThis file was sent using %@.",
[DCConfiguration getHumanReadableAppName] ];
[picker setMessageBody:emailBody isHTML:NO];
[self presentViewController:picker animated:true completion:^(void){}];
}
}
@catch (NSException *exception) {
NSLog(@"ContextMenuViewController sendMailTapped:%@",exception.description);
}
}
如果我重新启动 iPad 并打开应用程序,选择器将仅在第二次点击相应按钮时出现。
如果我在此之后再次单击该按钮,则每次第一次触摸时都会显示选择器并且它工作正常,直到我关闭并重新启动 iPad。
以下将打印到控制台:
Warning: Attempt to present <MFMailComposeViewController: 0x200cbb70> on <ContextMenuViewController: 0x200cd1a0> whose view is not in the window hierarchy!