Bringing up a dialog to send an email on iOS


#1

I’ve been trying to allow users to bring up the iOS mail dialog so they can send some of their user data as an attachment. My code works fine as long as their device is in portrait orientation, but if they’re in landscape orientation, the mail dialog still comes up in portrait orientation. And when you try to type an email address into the to field, the keyboard comes up in landscape orientation hiding the mail dialog so you can’t see what you’re typing.

I read that by default the MFMailComposeViewController class only allows portrait orientation, and I’ve been trying to override its default behavior. But I’ve had no luck figuring this out as of yet. Does any know what I’m doing wrong here:

class Emailer::Pimpl
{
public:
    Pimpl(Component* parent) 
      : parent(parent) {}

    ~Pimpl()
    {
    }

    bool sendFile(const File& file, const String& subject, const String& mimeType)
    {
        RotatableMailComposeViewController* mailViewController = [[RotatableMailComposeViewController alloc] init];

        if (mailViewController == nil)
            return false;

        [mailViewController setSubject:(NSString*) subject.toCFString()];

        // Attach an image to the email
        NSString* zipFilePath = (NSString*) file.getFullPathName().toCFString();
        NSString* zipFileName = (NSString*) file.getFileName().toCFString();
        NSData * zipData = [NSData dataWithContentsOfFile:zipFilePath];
        [mailViewController addAttachmentData:zipData mimeType:(NSString*) mimeType.toCFString() fileName:zipFileName];
        NSString * body = @"";
    
        // Fill out the email body text
        [mailViewController setMessageBody:body isHTML:NO];
    
        ComponentPeer* peer = parent->getPeer();
        if (peer != NULL)
        {
            UIView* view = (UIView*) peer->getNativeHandle();

            id nextResponder = [view nextResponder];
            if ([nextResponder isKindOfClass:[UIViewController class]]) 
            {
                UIViewController* controller = (UIViewController*) nextResponder;

                delegate = [[EmailerDelegate alloc] init];
                [delegate setUIViewController:controller];
                mailViewController.mailComposeDelegate = delegate;

                //[mailViewController attemptRotationToDeviceOrientation];

                // The method presentModalViewController in UIViewController is deprecated in iOS 6.
                float iOSPlatform6 = 6.0;
                if ([[[UIDevice currentDevice] systemVersion] floatValue] >= iOSPlatform6)
                {
                    [controller presentViewController:mailViewController animated:YES completion:NULL];
                }
                else
                {
                    [controller presentModalViewController:mailViewController animated:YES];
                }
            }
        }

        [mailViewController release];

        return true;
    }

    Component* parent;
    EmailerDelegate* delegate;
};
@interface RotatableMailComposeViewController : MFMailComposeViewController {
}
@end

@implementation RotatableMailComposeViewController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // should support all interface orientations.
    return true;
}

-(BOOL)shouldAutorotate{
    return true;
}

-(NSInteger)supportedInterfaceOrientations {
#ifdef __IPHONE_6_0
    NSInteger mask = 0;
    mask |= UIInterfaceOrientationMaskLandscapeRight;
    mask |= UIInterfaceOrientationMaskLandscapeLeft;
    mask |= UIInterfaceOrientationMaskPortrait;
    mask |= UIInterfaceOrientationMaskPortraitUpsideDown;
    return mask;
#else
    NSInteger mask = 0;
    mask |= UIInterfaceOrientationPortrait;
    mask |= UIInterfaceOrientationPortraitUpsideDown;
    mask |= UIInterfaceOrientationLandscapeLeft;
    mask |= UIInterfaceOrientationLandscapeRight;
    return mask;
#endif
}
@end

#2

Did you get anywhere with this? I might be needing to do this for an iOS app soon.