Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MFMailComposeViewController Crashes because of Global Appearance Properties on iOS6

I am getting the following crash when I present a MFMailComposeViewController:

2013-11-08 11:04:05.963 <redacted>[7108:1603] *** Assertion failure in NSDictionary *_UIRecordArgumentOfInvocationAtIndex(NSInvocation *, NSUInteger, BOOL)(), /SourceCache/UIKit/UIKit-2380.17/UIAppearance.m:1118
2013-11-08 11:04:06.032 <redacted>[7108:1603] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unknown key, "NSColor" in title text attributes dictionary'

I've tracked it down to the following appearance setting in my AppDelegate's application:didFinishLaunchingWithOptions: method:

        [[UINavigationBar appearance] setTitleTextAttributes:
            @{NSForegroundColorAttributeName : [UIColor whiteColor]}];

Commenting that line out does the trick, but ruins the rest of the app, so I tried specifically setting the titleTextAttributes to an empty dictionary for the MFMailComposeViewController:

Attempt #1

        [[UINavigationBar appearanceWhenContainedIn:
            NSClassFromString(@"MFMailComposeViewController"), nil]
            setTitleTextAttributes:@{ }];

That results in the same crash. And

        [[UINavigationBar appearanceWhenContainedIn:
            NSClassFromString(@"MFMailComposeViewController"), nil]
            setTitleTextAttributes:nil];

also results in the same crash.

Attempt #2

I noticed that MFMailComposeViewController is a UINavigationController, so maybe the global appearance settings are localized to UIViewControllers inside a UINavigationController. I put together some code to figure out what view controllers are inside the MFMailComposeViewController:

        for (UIViewController *viewController in mailViewController.viewControllers) {
            NSLog(@"%@", NSStringFromClass([viewController class]));
        }

Which results in the output:

2013-11-08 11:04:05.936 <redacted>[7108:907] MFMailComposeInternalViewController

So I tried (even though it's bad practice to rely on Apple's private view controllers):

        [[UINavigationBar appearanceWhenContainedIn:
            NSClassFromString(@"MFMailComposeViewController"), nil]
            setTitleTextAttributes:@{ }];

And

        [[UINavigationBar appearanceWhenContainedIn:
            NSClassFromString(@"MFMailComposeViewController"), nil]
            setTitleTextAttributes:nil];

But that still results in the same crash!

Attempt #3

        // right before instantiating the MFMailComposeViewController
        [[UINavigationBar appearance] setTitleTextAttributes:@{ }];

And

        [[UINavigationBar appearance] setTitleTextAttributes:nil];

Then restoring the global appearance properties in the completion block of dismissViewController:animated:completion:

However, this approach didn't work either. Does anyone know how to set titleTextAttributes on the global UINavigationBar appearance without crashing MFMailComposeViewController?

like image 454
paulrehkugler Avatar asked Nov 08 '13 16:11

paulrehkugler


2 Answers

Try using UITextAttributeTextColor instead of NSForegroundColorAttributeName.

like image 158
Jonathan Arbogast Avatar answered Nov 15 '22 19:11

Jonathan Arbogast


Just extends UINavigationController class

@interface MyNavigationController : UINavigationController
@end

replace all your UINavigationController class with the new subclass and [appearanceWhenContainedIn:] in your app delegate

[UINavigationBar appearanceWhenContainedIn:[MyNavigationController class], nil].titleTextAttributes = @{ NSForegroundColorAttributeName : [UIColor whiteColor] };

after that your app will not crash.

like image 45
c0ming Avatar answered Nov 15 '22 21:11

c0ming