【问题标题】:MFMailComposeViewController throws a viewServiceDidTerminateWithError and then exits when using a custom title fontMFMailComposeViewController 抛出 viewServiceDidTerminateWithError 并在使用自定义标题字体时退出
【发布时间】:2012-09-19 09:39:38
【问题描述】:

我遇到了很长一段时间以来遇到的最奇怪的问题......而且我已经没有想法了。

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

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

现在是疯狂的部分。如果我切换到另一个也使用 MFMailComposeViewController 的应用程序并启动该应用程序,然后切换回我的应用程序并再次启动邮件编写器,它就可以正常工作。我无法解释。

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

我四处搜寻,似乎找不到遇到同样问题的其他人。有人有什么建议吗?

我已经链接了 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 外观委托的设置。我有它使用自定义字体,如果我将其关闭,它似乎可以工作......但为什么它会在 iPhone5 上工作......

【问题讨论】:

  • 从 UIActivityViewController 调用时遇到同样的问题...你找到解决办法了吗?
  • 不,但我确实确定还有其他因素影响它,因为我制作了一个示例项目,它只做了这个,没有别的,它工作得很好。但我从来没有弄清楚问题的根源。目前,我只是针对这种情况将字体更改为系统字体,其余的则保留。
  • @gplocke 回答你自己的问题
  • 我已将其归档为 rdar://13422715 (openradar.me/13422715)

标签: objective-c ios ios6


【解决方案1】:

将 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];
}];

【讨论】:

  • 最后两行应在短暂延迟后调用,否则问题仍然存在(在 iPhone 4 上测试)。添加延迟问题肯定是固定的。
  • 你是对的。这足以为我第一次解决此问题,但该错误最近再次出现。我将它移到了mailComposeController:didFinishWithResult:error: 回调,它再次修复了它。我将更新代码以反映。
  • 更新它以在演示调用中使用完成块。
  • 您似乎不需要在每次显示邮件编辑器时来回更改字体设置。为[UINavigationBar appearanceWhenContainedIn: [MFMailComposeViewController class], nil]设置一次系统字体不会做同样的事情吗?
【解决方案2】:

这个问题最近出现在我正在做的一个项目中。我不太喜欢上面的解决方法,所以我创建了以下(可能更简洁)的解决方法:

// 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],
 }];

【讨论】:

  • 这是一个干净的解决方案。比上面显示的解决方法要好得多。
  • 我同意 appearanceWhenContainedIn: 是一个更好的解决方案。不过有一个权衡。 a)您的解决方法代码现在与您的错误分开。 b) 在演示过程中,您的字体实际上不再是自定义字体。
【解决方案3】:

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

我的诊断是自定义字体加载时间过长,并且会触发等待栅栏的超时。

【讨论】:

    【解决方案4】:

    将其设为 ivar:

    MFMailComposeViewController *picker 
    

    然后在这一行之后:

    [self dismissModalViewControllerAnimated:YES];
    

    添加这个:

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

    这样选择器的释放直到下一个 runloop 循环才会发生。

    【讨论】:

      【解决方案5】:

      例如,当我们将小数值放入自定义 UINavigationBar 时,就会发生这种情况 [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(1.5, -1.5) forBarMetrics:UIBarMetricsDefault]; 将偏移值设置为 UIOffsetMake(1.0, -1.0) 这将起作用。 希望这会有所帮助。

      【讨论】:

        【解决方案6】:

        dberwick 的解决方法有点工作 - 作曲家不再自动取消自己,并且一旦您关闭消息作曲家,自定义导航栏标题字体设置就会恢复,但是它不会在消息中显示自定义字体作曲家自己。

        我只是讨厌这种变通方法使我的实际代码变得臃肿,所以这里有一个简单的方法可以将大部分代码移出:

        - (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 答案的评论,但不允许有这么多代码。)

        【讨论】:

          【解决方案7】:

          我有同样的问题,但我认为我通过继承 UINavigationBar 解决了它。我更改了子类的外观,而不是 UINavigationBar。

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

          【讨论】:

            【解决方案8】:

            只需将作曲家添加为 iVar 即可解决我的问题。

            MFMailComposeViewController *emailComposer;

            【讨论】:

              【解决方案9】:

              根据我的阅读,在 iOS 6 中,这已被弃用:

              [self presentModalViewController:picker animated:YES];
              

              他们建议使用:

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

              与(didFinishWithResult)配对

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

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

              【讨论】:

                猜你喜欢
                • 2012-08-23
                • 2014-05-03
                • 1970-01-01
                • 1970-01-01
                • 2011-03-09
                • 2011-11-23
                • 2013-08-06
                • 2013-03-04
                • 1970-01-01
                相关资源
                最近更新 更多