Bruno Bernardino
Bruno Bernardino

Reputation: 496

iOS 7: Blank Screen when presenting MFMailComposeViewController on navigationController

I've got an app built using a storyboard, with a navigationController, and so far the only problem I'm having that I can't overcome is presenting a screen to send an email with a CSV attachment.

As far as I looked around, everything should be fine, and the app doesn't crash, it simply presents me a white screen when it should be showing me the mail composer view controller.

In my ListViewController.h (which is not the main controller, but a MainViewController.h is), I've got:

#import <UIKit/UIKit.h>
#import "MainViewController.h"
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>

@interface ListViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, UIPickerViewDelegate, UIActionSheetDelegate, MFMailComposeViewControllerDelegate>

...

- (IBAction) exportCSVToEmail;
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error;

@end

In my ListViewController.m, I've got:

@implementation ListViewController

...

// Export CSV to email and send it
- (IBAction) exportCSVToEmail
{
    ...

    if ( [MFMailComposeViewController canSendMail] ) {
        MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
        mailComposeViewController.mailComposeDelegate = self;
        [mailComposeViewController setSubject:emailSubject];
        [mailComposeViewController setMessageBody:emailBody isHTML:NO];
        [mailComposeViewController addAttachmentData:textFileContentsData mimeType:@"text/csv" fileName:csvFileName];
        [mailComposeViewController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        [self.navigationController presentViewController:mailComposeViewController animated:YES completion:^{
            //[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
            NSLog(@"Completed showing the mail view!");
        }];
    } else {
        ...
    }
}

#pragma mark MFMailComposeViewControllerDelegate
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    NSLog(@"Finished sending email!");
    [self dismissViewControllerAnimated:YES completion:nil];
}

...

I'm trying this on my device (I've got mail accounts set).

The problem does not appear to be in the attachment or anything like that (I've tried commenting things out, using a simplified version, and the problem was the same).

It looks like I'm either not presenting the view controller in the right place, or I'm missing something in the storyboard?

I do see the "Completed showing the mail view!" in the console, and no error at all, just a blank, white screen (with the status bar black, and I can customize it with the commented line).

This is intended to work on iOS7 only, btw.

Help? :/

UPDATE:

After some more attempts and suggestions from other users, it seems that no matter what viewController I put in that presentViewController:animated:completion, it always shows up a blank screen. The fact it's "blank" is related to the gradient I've got on the window's main layer, defined in my AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ...
    // Add background gradient
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.window.bounds;
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor colorWithRed:0.878431373 green:0.878431373 blue:0.878431373 alpha:1] CGColor], nil];
    [self.window.layer insertSublayer:gradient atIndex:0];
    ...
    return YES;
}

UPDATE 2:

Here's some settings on my storyboard... maybe I'm not seeing something...

storyboard screenshot #1 storyboard screenshot #2

UPDATE 3:

I've experimented with a simple app with a navigation controller and 1 view that launches email and that works without a problem. I'm trying to find out what it is in my app that's not allowing that to happen the same...

Upvotes: 0

Views: 520

Answers (2)

Bruno Bernardino
Bruno Bernardino

Reputation: 496

I've figured out the problem, thanks so much for your help @Vibin and @rdelmar!

The problem was in my AppDelegate.m, where I had:

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.window.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor colorWithRed:0.878431373 green:0.878431373 blue:0.878431373 alpha:1] CGColor], nil];
[self.window.layer insertSublayer:gradient atIndex:0];

It seems one can't do anything to the window for a storyboard application?

Anyway, now I'm out to probably add that gradient to each view controller instead of having it in the window :)

Thanks you so much!!

Upvotes: 1

Vibin B. Nair
Vibin B. Nair

Reputation: 49

I agree with rdelmar. I tried creating a sample app having a blank controller embedded in the navigation controller using storyboard. Mapped my controller class to the storyboard view controller and copy pasted your code in viewDidLoad of my controller class. I did not comment any of your line. I was able to see the Email screen, with all details populated, on the simulator.

For debugging purpose, can you try out one of the following :

(1). Try setting your email screen as the first screen(root view controller) for navigation controller in storyboard and load it.

(2). Pull out the code from ListViewController and move it to a seperate UIViewController class that just implements the MFMailComposeViewControllerDelegate and its functions. Map your email controller class to this new class and then try loading it.

Please share your observations.

Upvotes: 0

Related Questions