rohan panchal
rohan panchal

Reputation: 881

MFMailComposeViewController model does not display

I am presenting MFMailComposeViewController in my app for Email. But the view is not getting disply , only upper SEND and CANCEL buttons getting displayed.

This is the screen shot of view that is getting display after clicking on "Email to Subscribe" button.

enter image description here

I have checked out all the related codes. I have added "MessageUI" framework and imported

MessageUI/MessageUI.h , MessageUI/MFMailComposeViewController.h

I have used following code :

 - (void)viewWillAppear:(BOOL)animated {

     [self.view setFrame:CGRectMake(0, 62, 320, 418)];

     [APPDELEGATE.window addSubview:self.view];

     [self.navigationController.navigationBar setHidden:NO];
     self.navigationItem.title=@"Free Subscription";
     [super viewWillAppear:animated]; }


 -(void)viewWillDisappear:(BOOL)animated {
     [self.view removeFromSuperview];
     [super viewWillDisappear:animated]; }



     -(IBAction)btnEmailPressed {
         MFMailComposeViewController* Apicker = [[MFMailComposeViewController alloc] init];
         if (Apicker != nil)
         {

         [Apicker setSubject:@"Free Subscription"];

         [Apicker setMessageBody:@" " isHTML:NO];

         NSArray *toRecipients = [NSArray arrayWithObjects:@"[email protected]", nil];
         [Apicker setToRecipients:toRecipients];

         Apicker.mailComposeDelegate = self;

         if([MFMailComposeViewController canSendMail])
         {
             [self presentModalViewController:Apicker animated:YES];
         }
         else
         {

         }
         [Apicker release];
     } }


     -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError
     *)error{

     NSString *message;

     switch (result) {
         case MFMailComposeResultCancelled:
             message =@"You have canceled your email.";
             break;
         case MFMailComposeResultFailed:
             message=@"Your email is failed";
             break;
         case MFMailComposeResultSent:
             message=@"Your email was successfully sent.";
             break;
         case MFMailComposeResultSaved:
             message=@" Your email has been saved";
             break;

         default:
             message=@" Your email is not send";
             break;
     }
     UIAlertView *alt=[[UIAlertView alloc]initWithTitle:@"" message:message delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

     [alt show];
     [alt release];

     [self dismissModalViewControllerAnimated:YES];
      }

I could not get the reason for this problem.

Thanks.

Upvotes: 3

Views: 7136

Answers (3)

iAppDeveloper
iAppDeveloper

Reputation: 1088

Its for IOS 6

-(void)email{
    MFMailComposeViewController *composer=[[MFMailComposeViewController alloc]init];
    [composer setMailComposeDelegate:self];
    if ([MFMailComposeViewController canSendMail]) {
        [composer setToRecipients:[NSArray arrayWithObjects:@"[email protected]", nil]];
        [composer setSubject:@""];

        //    [composer.setSubject.placeholder = [NSLocalizedString(@"This is a placeholder",)];

        [composer setMessageBody:@"" isHTML:NO];
        [composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        [self presentViewController:composer animated:YES completion:nil];
    }
    else {
    }
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    if (error) {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"error" message:[NSString stringWithFormat:@"error %@",[error description]] delegate:nil cancelButtonTitle:@"dismiss" otherButtonTitles:nil, nil];
        [alert show];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

Just try

For iOS 5

-(void)email:(id)sender{
    MFMailComposeViewController *composer=[[MFMailComposeViewController alloc]init];
    [composer setMailComposeDelegate:self];
    if ([MFMailComposeViewController canSendMail]) {
        [composer setToRecipients:[NSArray arrayWithObjects:@"[email protected]", nil]];
        [composer setSubject:@""];

        //    [composer.setSubject.placeholder = [NSLocalizedString(@"This is a placeholder",)];

        [composer setMessageBody:@"" isHTML:NO];
        [composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        [self presentModalViewController:composer animated:YES];
    }
    else {
    }
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    if (error) {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"error" message:[NSString stringWithFormat:@"error %@",[error description]] delegate:nil cancelButtonTitle:@"dismiss" otherButtonTitles:nil, nil];
        [alert show];
        [self dismissModalViewControllerAnimated:YES];
    }
    else {
        [self dismissModalViewControllerAnimated:YES];
    }
}

Upvotes: 8

Chirag Bhutani
Chirag Bhutani

Reputation: 229

try modifying your code like this :-

-(IBAction)btnEmailPressed 
{ 
    MFMailComposeViewController* Apicker = [[MFMailComposeViewController alloc] init];
    if (Apicker != nil)   
    {
        [Apicker setSubject:@"Free Subscription"];
        [Apicker setMessageBody:@" " isHTML:NO];
        NSArray *toRecipients = [NSArray arrayWithObjects:@"[email protected]", nil];
        [Apicker setToRecipients:toRecipients];
        Apicker.mailComposeDelegate = self;

       if([MFMailComposeViewController canSendMail])
       {
           [self presentModalViewController:Apicker animated:YES];
       }
       else
       {
            NSString *recipients = @"mailto:[email protected]?cc=&subject=";
            NSString *body = @"&body=";

                NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
            email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

           [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
         }
         [Apicker release];
     } 
}

Upvotes: 2

Paras Joshi
Paras Joshi

Reputation: 20541

add these two delegate in .h file MFMessageComposeViewControllerDelegate and MFMailComposeViewControllerDelegate

UPDATE

     if ([MFMailComposeViewController canSendMail]) {
            appDelegate.imgCapture = [self captureView];
            [appDelegate.imgCapture retain];
            MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
            NSString *mailBody = @"Set Your Body Message";


            [mailComposeViewController setMessageBody:mailBody isHTML:NO];
            mailComposeViewController.mailComposeDelegate = self;
            [self presentViewController:mailComposeViewController animated:YES completion:nil];
        } else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"e-Mail Sending Alert"
                                                            message:@"You can't send a mail"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK" 
                                                  otherButtonTitles:nil];
            [alert show];
            [alert release];
        }

Upvotes: 0

Related Questions