Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing automatic MFMailComposeViewController opened from UITextView

I have a simple UITextView with an email link in it. The textview is selectable and detects links. This way, you can click on the email and it opens modally an MFMailComposeViewController view controller.

But, I do some customization at the launch of the app :

[[UINavigationBar appearance] setBarTintColor: myGreyColor];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: myFont}];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

This way, all navigation bars are grey, with white text and white buttons, and the title has a custom font.

My problem is that all these are not applied to the mail composer : the bar is grey and the title is white, but the font is the default helvetica neue and the buttons are the default blue. And, the status bar is black, even though my Info.plist says UIStatusBarStyleLightContent and View controller-based status bar appearance is set to NO.

I know how to customize MFMailComposeViewController when I call it manually, but here it pops up automatically. How can I have my styles applied to it ?

like image 381
rdurand Avatar asked Mar 03 '14 10:03

rdurand


1 Answers

EDIT

Customizing the MFMailComposeViewController's appearance is a really bad idea and will most likely get your app rejected by Apple. The following solution should only be used if you don't intend to submit your app to Apple.


Looks like I solved it, thanks to Ray Wenderlich (again..). Here is the full code :

- (void)viewDidLoad
{
    [super viewDidLoad];

    […] // Initializations

    // Link detection
    [_textView.attributedText addAttribute:NSLinkAttributeName value:@"mail://contact" range:[[content string] rangeOfString:@"[email protected]"]];

    _textView.delegate = self;

}

// Handle the link tap yourself
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {

    if ([[URL scheme] isEqualToString:@"mail"]) {

        MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
        [mailVC setToRecipients:@[@"contact@ mymail.com"]];
        [mailVC setSubject:@"About QuickReminder for iOS"];
        mailVC.mailComposeDelegate = self;

        // Re-set the styling
        [mailVC.navigationBar setBarTintColor:myGreyColor];
        [mailVC.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: myFont}];
        [mailVC.navigationBar setTintColor:[UIColor whiteColor]];

        [self presentViewController:mailVC animated:YES completion:^{
            [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        }];

        return NO;
    }
    return YES;
}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    // Notifies users about errors associated with the interface
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Result: canceled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Result: saved");
            break;
        case MFMailComposeResultSent:
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Result" message:@"Mail Sent Successfully" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Result: failed");
            break;
        default:
            NSLog(@"Result: not sent");
            break;
    }

    [controller dismissViewControllerAnimated:YES completion:nil];
}
like image 61
rdurand Avatar answered Nov 01 '22 01:11

rdurand