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

查看:12
本文介绍了点击 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 出现在屏幕外.如果您检查调试日志,您可能会看到一条消息,说 Prepresenting action sheet 被其超级视图剪辑.某些控件可能无法响应触摸. 在 iPhone 上尝试 -[UIActionSheet showFromTabBar:]-[UIActionSheet showFromToolbar:] 而不是 -[UIActionSheetshowInView:]."

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 并更改名为 Main nib file base (iPad) 的 Info.plist 条目>主 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天全站免登陆