7

Soy un desarrollador de software, y estoy haciendo una aplicación para el mensaje de correo electrónico y tengo el siguiente código:está enviando el mensaje de correo electrónico desde la aplicación utilizando MFMailComposeViewController en MessageUI en el iPhone

// Header file 

// importing the MessageUI framework 
#import <MessageUI/MessageUI.h> 

// adding the delegate functionality to the class (<MFMailComposeViewControllerDelegate>) 
@interface TutorialProjectViewController : UIViewController <MFMailComposeViewControllerDelegate> { 

} 

- (IBAction)pressTheMailButtonDudeFunction:(id)sender 

// Implementation file 

- (IBAction)pressTheMailButtonDudeFunction:(id)sender { 

    // allocatind new message composer window 
    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; 

    // setting a delegate method to "self" 
    mc.mailComposeDelegate = self; 

    // pre-populating the message subject 
    [mc setSubject:@"Send me a message"]; 

    // adding content of the message as a plain text 
    [mc setMessageBody:@"Send me a message is you like this tutorial :)" isHTML:NO]; 

    // adding content of the message as an HTML 
    [mc setMessageBody:@"<p>Send me a message is you like this tutorial :)<p>" isHTML:YES]; 

    // adding recipients 
    [mc setToRecipients:[NSArray arrayWithObjects:@"Fuerte <[email protected]>", @"[email protected]", nil]]; 

    // adding recipients for a send copy to (arrayWithObject or arrayWithObjects) 
    [mc setCcRecipients:[NSArray arrayWithObject:@"[email protected]"]]; 

    // adding hidden recipients 
    [mc setBccRecipients:[NSArray arrayWithObject:@"[email protected]"]]; 

    // adding image attachment 
    // getting path for the image we have in the tutorial project 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Extra_Xcode_100x100" ofType:@"png"]; 

    // loading content of the image into NSData 
    NSData *imageData = [NSData dataWithContentsOfFile:path]; 

    // adding the attachment to he message 
    [mc addAttachmentData:imageData mimeType:@"image/png" fileName:@"Collection"]; 

    // setting different than the default transition for the modal view controller 
    [mc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve]; 

    /* 
    Modal view controllers transitions: 

    UIModalTransitionStyleCoverVertical => pops up from the bottom, default transition 
    UIModalTransitionStyleCrossDissolve => fade on the screen 
    UIModalTransitionStyleFlipHorizontal => page flip 
    */ 

    // displaying our modal view controller on the screen (of course animated has to be set on YES if you want to see any transition) 
    [self presentModalViewController:mc animated:YES]; 

    // releasing the controller 
    [mc release]; 
} 

// delegate function callback 
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { 
    // switchng the result 
    switch (result) { 
     case MFMailComposeResultCancelled: 
      NSLog(@"Mail send canceled."); 
      /* 
      Execute your code for canceled event here ... 
      */ 
      break; 
     case MFMailComposeResultSaved: 
      NSLog(@"Mail saved."); 
      /* 
      Execute your code for email saved event here ... 
      */ 
      break; 
     case MFMailComposeResultSent: 
      NSLog(@"Mail sent."); 
      /* 
      Execute your code for email sent event here ... 
      */ 
      break; 
     case MFMailComposeResultFailed: 
      NSLog(@"Mail send error: %@.", [error localizedDescription]); 
      /* 
      Execute your code for email send failed event here ... 
      */ 
      break; 
     default: 
      break; 
    } 
    // hide the modal view controller 
    [self dismissModalViewControllerAnimated:YES]; 
} 

Y' No estoy recibiendo la respuesta correcta ... ¿Es un código correcto?

+0

¿qué respuesta tienes? – dks1725

+0

muestra demasiados errores que no puedo entender. Muestra "Correo enviado cancelado", "Correo guardado", "Correo enviado". al mismo tiempo, – Sweeta

+0

Sea un poco más elaborado sobre su problema. –

Respuesta

5
  1. Asegúrese de haber incluido el marco MessageUI en su proyecto iOS. Dentro de Xcode 4, puede incluir el marco seleccionando su proyecto en la columna de la izquierda. Luego seleccionando la pestaña "Build Phases". Aquí puede hacer clic en la flecha a la izquierda de "Enlace binario con bibliotecas" y verá la lista de marcos que ya están incluidos en su aplicación. Si MessageUI.framework falta, simplemente agrégalo allí.
  2. El código que ha publicado parece un código de tutorial completo cortado ... solo use el código que necesita ... y añada más funciones paso a paso. De esta forma, verá dónde agrega una línea de código defectuosa. Tal vez no haya una imagen "Extra_Xcode_100x100.png" en su paquete de aplicaciones.

Por lo tanto, aquí hay un MFMailComposeViewController "mínimo":


- (IBAction)showMinimalModalMailView:(id)sender { 
    // get a new new MailComposeViewController object 
    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; 

    // his class should be the delegate of the mc 
    mc.mailComposeDelegate = self; 

    // set a mail subject ... but you do not need to do this :) 
    [mc setSubject:@"This is an optional mail subject!"]; 

    // set some basic plain text as the message body ... but you do not need to do this :) 
    [mc setMessageBody:@"This is an optional message body plain text!" isHTML:NO]; 

    // set some recipients ... but you do not need to do this :) 
    [mc setToRecipients:[NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]]; 

    // displaying our modal view controller on the screen with standard transition 
    [self presentModalViewController:mc animated:YES]; 

    // be a good memory manager and release mc, as you are responsible for it because your alloc/init 
    [mc release]; 

} 
2

tenido han tenido el mismo problema, cada vez que ejecute la aplicación en enviar el mensaje de que se estrelle. He descubierto que si se quita

[mc setToRecipients:[NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]];

línea que funciona bien y sólo me pide la dirección de correo electrónico.

0

Para enviar un mensaje de correo electrónico desde la aplicación, su dispositivo debe configurarse para el servicio de correo electrónico.

// importing the MessageUI framework 
#import <MessageUI/MessageUI.h> 

// adding the delegate functionality to the class (<MFMailComposeViewControllerDelegate>) 
@interface ViewController : UIViewController <MFMailComposeViewControllerDelegate> { 

} 

- (IBAction)sendEMailClick:(id)sender { 

    //check mail service is configure to your device or not. 
    if ([MFMailComposeViewController canSendMail]) { 

     // get a new new MailComposeViewController object 
     MFMailComposeViewController * composeVC = [MFMailComposeViewController new]; 

     // his class should be the delegate of the composeVC 
     [composeVC setDelegate:self]; 

     // set a mail subject ... but you do not need to do this :) 
     [composeVC setSubject:@"This is an optional mail subject!"]; 

     // set some basic plain text as the message body ... but you do not need to do this :) 
     [composeVC setMessageBody:@"This is an optional message body plain text!" isHTML:NO]; 

     // set some recipients ... but you do not need to do this :) 
     [composeVC setToRecipients:[NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]]; 

     // Present the view controller modally. 

     [self presentViewController:composeVC animated:true completion:nil]; 
    } else { 

     NSLog(@"Mail services are not available or configure to your device"); 
    } 
} 

después de correo electrónico es enviar o cancelar haga clic en MFMailComposeViewController delegado méthode de MFMailComposeViewControllerDelegate es llamada, por lo que no se puede consultar el correo electrónico enviar el estado.

#pragma mark - MFMailComposeViewControllerDelegate Methode. 
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(nullable NSError *)error { 

    switch (result) { 
     case MFMailComposeResultCancelled: 
      NSLog(@"Mail cancelled"); 

      break; 

     case MFMailComposeResultSaved: 
      NSLog(@"Mail saved"); 

      break; 

     case MFMailComposeResultSent: 
      NSLog(@"Mail sent"); 

      break; 

     case MFMailComposeResultFailed: 
      NSLog(@"Mail sent failure: %@",error.description); 

      break; 
    } 

    // Dismiss the mail compose view controller. 
    [controller dismissViewControllerAnimated:true completion:nil]; 

} 
Cuestiones relacionadas