Search code examples
iosobjective-cmfmailcomposeviewcontroller

EMail sharing by MFMailComposeViewController in objective c not work properly for IOS 9.0. ToRecipients and send option is not worked


I am trying to open email composure in iOS 9 and send option is not working. I have downloaded some project they also have the same problem with ios 9.

I have declared globalMailComposer in app delegate .h and this code is in app delegate .m file in a alert buton

if ([MFMailComposeViewController canSendMail])
{

    globalMailComposer = [[MFMailComposeViewController alloc] init];
    self.globalMailComposer.mailComposeDelegate = self;
    NSString *emailTitle = @"Test Email";
    NSString *messageBody = @"iOS programming is so fun!";
    NSArray *toRecipents:    [NSArrayarrayWithObjects:@"[email protected]",nil];
    [globalMailComposer setSubject:emailTitle];
    [globalMailComposer setMessageBody:messageBody isHTML:NO];
    [globalMailComposer setToRecipients:toRecipents];
    [self.window.rootViewControllerpresentViewController:globalMailComposer animated:YES completion:nil];
}


- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@", [error localizedDescription]);
            break;
        default:
            break;
    }
    [self.window.rootViewController dismissViewControllerAnimated:YES  completion:NULL];
}

Solution

    1. You cannot send mails from within the simulator. Always test this on a real device.
    2. Try this code on your device which works perfectly fine for me:

    AppDelegate.h

    #import <UIKit/UIKit.h>
    @import MessageUI;
    
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    
    @property (strong, nonatomic) UIWindow *window;
    @property (nonatomic, strong) MFMailComposeViewController *globalMailComposer;
    
    @end
    

    AppDelegate.m

    #import "AppDelegate.h"
    
    @interface AppDelegate () <MFMailComposeViewControllerDelegate>
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
        if ([MFMailComposeViewController canSendMail])
        {
            self.globalMailComposer = [[MFMailComposeViewController alloc] init];
            self.globalMailComposer.mailComposeDelegate = self;
            NSString *emailTitle = @"Test Email";
            NSString *messageBody = @"iOS programming is so fun!";
            [self.globalMailComposer setSubject:emailTitle];
            [self.globalMailComposer setMessageBody:messageBody isHTML:NO];
            [self.globalMailComposer setToRecipients:@[@"[email protected]"]];
            [self.window.rootViewController presentViewController:self.globalMailComposer animated:YES completion:nil];
        }
    
        return YES;
    }
    
    - (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
    {
        switch (result)
        {
            case MFMailComposeResultCancelled:
                NSLog(@"Mail cancelled");
                break;
            case MFMailComposeResultSaved:
                NSLog(@"Mail saved");
                break;
            case MFMailComposeResultSent:
                NSLog(@"Mail sent");
                break;
            case MFMailComposeResultFailed:
                NSLog(@"Mail sent failure: %@", [error localizedDescription]);
                break;
            default:
                break;
        }
        [self.window.rootViewController dismissViewControllerAnimated:YES  completion:NULL];
    }
    
    @end
    
    1. If it's still not working make sure you have set up the mail account on your device correctly.It should appear in your Settings under Mail-Accounts.