MFMailComposeViewController抛出一个viewServiceDidTerminateWithError,然后在使用自定义标题字体时退出

我遇到了很久以来遇到的最奇怪的问题,而且我已经没有想法了。

所以我有一个MFMailComposeViewController是通过在UIButton上点击而启动的,它启动邮件编辑器视图就好了。 你看到我分配的主题,但在to:或body字段被填充之前,窗口会闪烁并消失。 它会抛出这个错误:

viewServiceDidTerminateWithError:Error Domain = XPCObjectsErrorDomain Code = 2“操作无法完成(XPCObjectsErrorDomain错误2)”

现在,这是疯狂的一部分。 如果我切换到另一个也使用MFMailComposeViewController的应用程序,并启动该应用程序,然后切换回我的应用程序,并再次启动邮件编辑器,它工作得很好。 我无法解释这一点。

这似乎只是运行iOS 6的手机不是 iPhone 5的问题。

我已经search了四周,似乎无法find谁遇到了同样的问题的其他人。 任何人有任何build议?

我已经得到了MessageUI.framework的链接,我也发现这不是在模拟器或设备上工作,但是当我也链接Security.framework它开始工作在模拟器,但它仍然无法正常工作在设备上。

我启动MFMailComposeViewController的代码如下:

在.h文件中

#import <MessageUI/MessageUI.h> #import <MessageUI/MFMailComposeViewController.h> 

在.m文件中

 -(void)displayComposerSheet { MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; [picker setSubject:@"Support Request"]; // Set up recipients NSArray *toRecipients = [NSArray arrayWithObject:@"support@domain.com"]; [picker setToRecipients:toRecipients]; // Fill out the email body text NSString *emailBody = @"\n\nEmail from iOS"; [picker setMessageBody:emailBody isHTML:NO]; [self presentModalViewController:picker animated:YES]; } // Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation. - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { [self dismissModalViewControllerAnimated:YES]; } 

更新:我想我已经缩小到我已经传递给UINavigationBar的外观委托的设置。 我有它使用自定义字体,如果我把它closures似乎工作…但为什么会在iPhone5上工作…

将UITextAttributeFont的自定义字体设置为UINavigationBar外观代理的titleTestAttributes会导致该错误与OP和MightlyLeader标识一致。

解决方法代码:

 // remove the custom nav bar font NSMutableDictionary* navBarTitleAttributes = [[UINavigationBar appearance] titleTextAttributes].mutableCopy; UIFont* navBarTitleFont = navBarTitleAttributes[UITextAttributeFont]; navBarTitleAttributes[UITextAttributeFont] = [UIFont systemFontOfSize:navBarTitleFont.pointSize]; [[UINavigationBar appearance] setTitleTextAttributes:navBarTitleAttributes]; // set up and present the MFMailComposeViewController MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init]; mailComposer.mailComposeDelegate = self; [mailComposer setSubject:emailInfo[@"subject"]]; [mailComposer setMessageBody:emailInfo[@"message"] isHTML:YES]; mailComposer.modalTransitionStyle = UIModalTransitionStyleCoverVertical; [self presentViewController:mailComposer animated:YES completion:^{ // add the custom navbar font back navBarTitleAttributes[UITextAttributeFont] = navBarTitleFont; [[UINavigationBar appearance] setTitleTextAttributes:navBarTitleAttributes]; }]; 

这个问题最近出现在我正在进行的一个项目上。 我真的不喜欢上面的解决方法,所以我创build了以下(也许是一个小清洁工)的解决方法:

 // Implement the custom font for all UINavigationBar items [[UINavigationBar appearance] setTitleTextAttributes: @{ UITextAttributeFont : [UIFont custom_superAwesomeFontWithSize:16.0f], }]; // Disable the custom font when the NavigationBar is presented in a MFMailComposeViewController [[UINavigationBar appearanceWhenContainedIn:[MFMailComposeViewController class], nil] setTitleTextAttributes: @{ UITextAttributeFont : [UIFont boldSystemFontOfSize:14.0f], }]; 

我遇到过同样的问题。 我已经将标题栏的文本属性设置为自定义字体。 当我删除自定义字体规范(但将所有其他属性保留为自定义值)时,问题消失。

我的诊断是自定义字体加载时间过长,并从等待屏幕触发超时。

使这个伊娃:

 MFMailComposeViewController *picker 

然后在这一行之后:

 [self dismissModalViewControllerAnimated:YES]; 

添加这个:

 dispatch_async(dispatch_get_main_queue(), ^{ picker = nil; }); 

这样才能在下一个runloop循环之前发布选取器。

当我们在自定义的UINavigationBar中input小数值时,会发生这种情况,例如[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(1.5, -1.5) forBarMetrics:UIBarMetricsDefault]; 设置偏移值为UIOffsetMake(1.0,-1.0),这将工作。 希望这可以帮助。

dberwick的解决方法有点作品 – composer php不再自动取消自己,一旦closures了消息编辑器,自定义的导航栏标题字体设置就会恢复, 但是它不会在消息编辑器自身中显示自定义字体。

我只是讨厌这个解决方法如何臃肿我的实际代码,所以这里有一个简单的方法来移动它的大部分:

 - (void)presentMessageCommposer void (^workaroundRestoreFont)(void) = [self ym__workaroundCustomFontInMessageComposer]; MFMailComposeViewController *mailComposeVC = [MFMailComposeViewController new]; // ... set up the composer: message body, subject, etc ... [self presentViewController:mailComposeVC animated:YES completion:workaroundRestoreFont]; } // ugly workaround stuff // move this to the bottom of your class, collapse it, or put it in a category - (void (^)(void))ym__workaroundCustomFontInMessageComposer { // Bug http://openradar.appspot.com/13422715 // remove the custom nav bar font NSMutableDictionary* navBarTitleAttributes = [[UINavigationBar appearance] titleTextAttributes].mutableCopy; UIFont *navBarTitleFont = navBarTitleAttributes[UITextAttributeFont]; navBarTitleAttributes[UITextAttributeFont] = [UIFont systemFontOfSize:navBarTitleFont.pointSize]; [[UINavigationBar appearance] setTitleTextAttributes:navBarTitleAttributes]; return ^{ // add the custom navbar font back NSMutableDictionary* navBarTitleAttributes = [[UINavigationBar appearance] titleTextAttributes].mutableCopy; navBarTitleAttributes[UITextAttributeFont] = navBarTitleFont; [[UINavigationBar appearance] setTitleTextAttributes:navBarTitleAttributes]; }; } 

(这应该是对dberwick答案的一个评论,但是这不会允许这么多的代码。)

我有同样的问题,但我想我通过inheritanceUINavigationBar来解决它。 我改变了我的子类,而不是UINavigationBar的外观。

 [[MYNavigationBar appearance] setTitleTextAttributes:@{ UITextAttributeFont : [UIFont fontWithName:@"Custom Font" size:25] }]; 

简单地添加composer php作为iVar解决了我的问题。

MFMailComposeViewController * emailComposer;

从我读过的内容来看,在iOS 6中,这已经被弃用了:

 [self presentModalViewController:picker animated:YES]; 

他们build议使用:

 [self presentViewController:picker animated:YES completion:nil]; 

配对(在didFinishWithResult

 [[controller presentingViewController] dismissViewControllerAnimated:YES completion:nil]; 

不幸的是,这只能在模拟器上间歇性地工作,但它有时候还是有效的!