Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

iphone - Action sheet doesn't display when the MFMailComposeViewController's cancel button is tapped

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.

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.

My code:

  -(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:@"[email protected]"]; 

     [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:[email protected][email protected],[email protected]&subject=illusions!";
NSString *body = @"&body=xyz";

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

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Method

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

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

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:]."

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

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).

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!

Hope it helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...