3

我在我的电子邮件应用程序中展示 MFMailComposeViewController。但是视图没有得到显示,只显示上部的 SEND 和 CANCEL 按钮。

这是单击“发送电子邮件至订阅”按钮后显示的视图屏幕截图。

在此处输入图像描述

我已经检查了所有相关的代码。我添加了“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];
      }

我无法得到这个问题的原因。

谢谢。

4

3 回答 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];
    }
}
于 2012-11-29T10:47:21.263 回答
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];
     } 
}
于 2012-11-29T10:52:18.887 回答
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];
        }
于 2012-11-29T10:27:24.983 回答