点击MFMailComposeViewController的取消按钮时,不显示操作表 [英] Action sheet doesn't display when the MFMailComposeViewController's cancel button is tapped

查看:96
本文介绍了点击MFMailComposeViewController的取消按钮时,不显示操作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用中加入 MFMailComposeViewController 。当我以模态方式呈现它时,发送按钮工作正常并且发送电子邮件,这意味着在这种情况下发送给代表的结果是正确的。

I'm trying to incorporate MFMailComposeViewController in my app. When I present it modally, the send button works fine and the email is sent, which implies that the result sent to the delegate is right in that case.

而当我点击取消按钮它会挂起应用程序。日志也没有显示错误,只是屏幕变暗,一切都被禁用。显然,结果没有传递给委托(我通过日志检查)。看来

Whereas when I tap the cancel button it hangs up the app. The log shows no errors either, just the screen goes dark and everything gets disabled. Apparently, the result is not being passed to the delegate (I checked it through logs). it appears that the

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

只要按下取消按钮,就不会调用

。可能这就是为什么没有显示动作表(保存草稿取消删除草稿)的原因,因此应用程序就会挂起。

is never called whenever the cancel button is pressed. Probably that's the reason why the actionsheet (Save draft, cancel, delete draft) is not displayed and therefore the app hangs in right there.

我正在使用Apple的示例应用程序( MailComposer )中的确切代码,它在那里工作得很好,但在某种程度上我的失败了。 :(

I'm using the exact code from Apple's sample apps (MailComposer), it works perfectly there, but somehow fails in mine. :(

如果有人遇到同样的问题并成功解决了问题,请帮助我。

Kindly help me if anyone has ever come across the same issue, and successfully resolved it.

我的代码:

  -(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 
 {  

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


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


推荐答案

方法

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

永远不会被调用,因为你没有按任何 UIActionSheet 按钮,因为屏幕上没有显示。

is never called because you don't press any UIActionSheet button after cancelling, as it doesn't show on screen.

发生这种情况的原因是 UIActionSheet 出现在屏幕外。如果您检查调试日志,您可能会看到一条消息,指出显示由其超级视图剪切的操作表。某些控件可能无法响应触摸。在iPhone上尝试 - [UIActionSheet showFromTabBar:] - [UIActionSheet showFromToolbar:] 而不是 - [UIActionSheet showInView:]

The reason this is happening is that the UIActionSheet appears off-screen. If you check the debug log you'll probably see a message saying Presenting action sheet clipped by its superview. Some controls might not respond to touches. On iPhone try -[UIActionSheet showFromTabBar:] or -[UIActionSheet showFromToolbar:] instead of -[UIActionSheet showInView:]."

这就是为什么你看到你的视野变暗,但没有 UIActionSheet 出现。

That's why you see your view getting darker, but no UIActionSheet appears.

在我的情况下,问题是我的应用程序是通用的,但对于某些人来说原因只有一个 MainWindow.xib ,它大于iPhone屏幕尺寸(实际上是iPad的屏幕尺寸)。

In my case, the problem was that my app is universal, but for some reason there was only one MainWindow.xib, and it was larger than the iPhone screen size (it was, in fact, the iPad screen size).

解决方案是使用正确的尺寸创建另一个 MainWindow-iPhone.xib 并更改名为主笔尖的Info.plist条目文件库(iPad)主nib文件库(iPhone),以便它们指向正确的文件。问题解决了!

The solution is to create another MainWindow-iPhone.xib with the right dimensions and change the Info.plist entries called Main nib file base (iPad) and Main nib file base (iPhone) so that they point to the right file. Problem solved!

希望它有所帮助。

这篇关于点击MFMailComposeViewController的取消按钮时,不显示操作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆