MFMailComposeViewController模态框未显示。

3
我在我的应用程序中使用MFMailComposeViewController发送电子邮件。但是视图没有显示,只有上方的SEND和CANCEL按钮显示出来。
这是点击“Email to Subscribe”按钮后显示的视图截图。 enter image description here 我已经检查了所有相关代码。我添加了“MessageUI”框架并导入了它。
MessageUI/MessageUI.h , MessageUI/MFMailComposeViewController.h

我用了以下代码:

我使用了以下代码:

 - (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:@"info@xyz.com", 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];
      }

我无法理解这个问题的原因。
谢谢。

@PuneethSB - 嗯,我已经添加了它。 - rohan panchal
我将粘贴代码,这些代码对我而言是有效的。 - Master Stroke
你是使用Xib还是通过编程创建视图? - Master Stroke
[APPDELEGATE.window addSubview:self.view]; 为什么要将视图添加到窗口中?我不明白,你能解释一下吗? - Master Stroke
你在接口中添加了协议吗? - Midhun MP
显示剩余5条评论
3个回答

8

这是针对IOS 6的。

-(void)email{
    MFMailComposeViewController *composer=[[MFMailComposeViewController alloc]init];
    [composer setMailComposeDelegate:self];
    if ([MFMailComposeViewController canSendMail]) {
        [composer setToRecipients:[NSArray arrayWithObjects:@"xyz@gmail.com", 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];
    }
}

试一试

iOS 5版本

-(void)email:(id)sender{
    MFMailComposeViewController *composer=[[MFMailComposeViewController alloc]init];
    [composer setMailComposeDelegate:self];
    if ([MFMailComposeViewController canSendMail]) {
        [composer setToRecipients:[NSArray arrayWithObjects:@"xyz@gmail.com", 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];
    }
}

谢谢回复。但是如果是针对iOS 5呢? - rohan panchal
谢谢您的回复。您能否说明一下iOS 6和iOS 5代码之间的区别? - rohan panchal
@rohanpanchal 显示presentmodalviewcontroller和dismissModalViewcontroller。 - Master Stroke
我已经尝试了你所有的代码和其他的代码,但是都没有影响结果。 :( - rohan panchal

2
尝试按照以下方式修改您的代码:-
-(IBAction)btnEmailPressed 
{ 
    MFMailComposeViewController* Apicker = [[MFMailComposeViewController alloc] init];
    if (Apicker != nil)   
    {
        [Apicker setSubject:@"Free Subscription"];
        [Apicker setMessageBody:@" " isHTML:NO];
        NSArray *toRecipients = [NSArray arrayWithObjects:@"info@xyz.com", nil];
        [Apicker setToRecipients:toRecipients];
        Apicker.mailComposeDelegate = self;

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

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

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

处理 canSendMail 返回 NO 的好的替代方案。 - Steve Moser

0
.h文件中添加这两个代理:MFMessageComposeViewControllerDelegateMFMailComposeViewControllerDelegate

更新

     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];
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接