【问题标题】:How to present an MFMailComposeViewController in an already modalViewController?如何在已经 modalViewController 中呈现 MFMailComposeViewController?
【发布时间】:2011-05-15 13:13:45
【问题描述】:

我有一个 modalViewController,带有一个名为 email 的按钮。按下此按钮后,它会以模态方式显示 MFMailComposeViewController。当我发送电子邮件时,一切正常。但是当用户点击 MFMail 视图上的取消按钮时,应用程序挂断并且不显示操作表。它在控制台等中没有显示任何细节,显然在这种情况下不会调用 MFMailCompose 委托。

我在另一个视图控制器(不是模态视图控制器)中使用相同的代码进行了检查,发现那里一切正常。因此,我认为问题在于在已经是 modalviewcontroller 的控制器中显示 MFMailComposeViewController。

很久以来我一直在寻找一些帮助,得出的结论是,由于 MFMailComposeViewController 本身就是一个模态视图控制器,因此它只能在简单的视图控制器上使用,而不能在模态视图控制器中工作。

有人可以指导我如何在 modalviewcontroller 中使用 MFMailComposer。任何帮助将不胜感激。

我的代码:

    -(IBAction)emailButtonPressed:(id)sender{

           Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
       if (mailClass != nil)
          {

          if ([mailClass canSendMail])
            {
              [self displayComposerSheet];
            }
          else
            {
              [self launchMailAppOnDevice];
            }
          }
        else
          {
            [self launchMailAppOnDevice];
          }


}


#pragma mark -
#pragma mark Compose Mail


-(void)displayComposerSheet 
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    [picker setSubject:@"Ilusiones"];


    // Set up recipients
     NSArray *toRecipients = [NSArray arrayWithObject:@"anam@semanticnotion.com"]; 

     [picker setToRecipients:toRecipients];
     // Attach a screenshot to the email      
     UIGraphicsBeginImageContext(self.view.bounds.size);
     [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
     UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();

         NSData *myData = UIImagePNGRepresentation(viewImage);
     [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"viewImage"];



     // Fill out the email body text
     NSString *emailBody = @"";
     [picker setMessageBody:emailBody isHTML:NO];

     [self presentModalViewController:picker animated:YES];
         [picker release];

 }



 - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
 {  
   NSLog (@"entered the delegate"); // this line is not shown when the cancel button is tapped-meaning it never enters the delegate in that case.
  switch (result)
  {
case MFMailComposeResultCancelled:
    NSLog(@"Result: canceled");
    break;
case MFMailComposeResultSaved:
    NSLog(@"Result: saved");
    break;
case MFMailComposeResultSent:
    NSLog( @"Result: sent");
    break;
case MFMailComposeResultFailed:
    NSLog( @"Result: failed");
    break;
default:
    NSLog(@"Result: not sent");
    break;
 }
 [self dismissModalViewControllerAnimated:YES]; //this works fine in the send case, and the email is sent. but hangs in the cancel case.
}


#pragma mark -
#pragma mark Workaround


-(void)launchMailAppOnDevice
{
NSString *recipients = @"mailto:anam@semanticnotion.com.com?cc=second@example.com,third@example.com&subject=illusions!";
NSString *body = @"&body=xyz";

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

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

【问题讨论】:

    标签: iphone objective-c ios4


    【解决方案1】:

    两种方式都对我有用。确保您已将您的 VC 声明为 MFMailComposeViewController 的委托:

    @interface MyViewController : UIViewController <MFMailComposeViewControllerDelegate>
    

    Canceled 的索引是 0 顺便说一句:

    http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMailComposeViewController_class/Reference/Reference.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 2017-05-24
      • 1970-01-01
      相关资源
      最近更新 更多