After sending an in App email with photo attachment the Email window won't dismiss even when I hit send or cancel

StackOverflow https://stackoverflow.com/questions/13629390

Domanda

I'm in a bit of trouble, I've spent half a day working onto this issue I have with no major results after all deprecations in iOS6 and other issues. This iOS app, has a send email option when after pushing a button, the app takes a screenshot of my WebView, attaches it to the email and from there, have the normal options to cancel or send the email and return to the app. I made it to the part where the email pops up, and actually 2 issues here: one is that after pressing either cancel or send, the email view won't dismiss, the app is stuck in the email view. And the second issue I have is the image that gets attached is just a tiny icon (blue with an question mark just like it is not recognised or is missing... Can someone please point me in the right direction as I feel like I'm going crazy. I've researched the net backwards and forwards with no luck. Many similar threads but different issues not exactly related to my issues unfortunately. Sorry, and thanks in advance. Here is my code:

// in my LiveView.h file
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <MessageUI/MessageUI.h>
#import "CamSetup.h"

@interface LiveView : UIViewController < MFMailComposeViewControllerDelegate ,   ADBannerViewDelegate >  
....
-(IBAction)shotAndSend:(id)sender;


// in my LiveView.m file:


- (void)mailComposer:(MFMailComposeViewController *)controller
      didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error 
{
NSLog(@"in didFinishWithResult:");
switch (result) 
   {
    case MFMailComposeResultCancelled:
        NSLog(@"cancelled");
        break;
    case MFMailComposeResultSaved:
        NSLog(@"saved");
        break;
    case MFMailComposeResultSent:
        NSLog(@"sent");
        break;
    case MFMailComposeResultFailed: {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error sending mail",@"Error sending mail")
        message:[error localizedDescription] delegate:nil
                                              cancelButtonTitle:NSLocalizedString(@"test",@"test")
                                              otherButtonTitles:nil];
        [alert show];
        break;
    }
    default: break;
 }
  [self dismissViewControllerAnimated:NO completion:Nil];
   }

 -(IBAction)shotAndSend:(id)sender
{
   UIGraphicsBeginImageContext(_myWebView.frame.size);
   [_myWebView.layer renderInContext:UIGraphicsGetCurrentContext()];
   UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   NSData * imageData = UIImageJPEGRepresentation (image, 2.1);

  if ([MFMailComposeViewController canSendMail])
  {
     MFMailComposeViewController * mailComposer = [[MFMailComposeViewController alloc] init];
     mailComposer.mailComposeDelegate = self;
    [mailComposer addAttachmentData:imageData mimeType:@"image/jpeg" fileName:@"attachment.jpeg"];
    [mailComposer setSubject:@"A screenshot from my App"];
    [mailComposer setToRecipients:[NSArray arrayWithObjects:@"123@yexample.com", nil]];
    [self presentViewController: mailComposer animated:YES completion:NULL];
 }
 }
È stato utile?

Soluzione

Your code isn't working because you have the wrong method. You have:

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

but it should be:

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

Altri suggerimenti

You need a delegate of your view that will dismiss that view for you.
The fact that you don't see the log indicate you that you haven't implemented correctly the delegation protocol. Take a look at some example in the apple documentation.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top