how to test MFMailComposeViewController in simulator

前端 未结 3 1010
忘了有多久
忘了有多久 2021-01-25 17:38

Is there a way to test sending emails from MFMailComposeViewController in iphone simulator ?

相关标签:
3条回答
  • 2021-01-25 18:18

    No you can't test it on simulator...(I mean your mail won't be delivered).we will be able to test limited things like: how the view will be,what happens when the user clicks on cancel button etc...

    To test whether, the mail was delivered or not, you have to use a Device. The device should be configured with some mail(ex:gmail) in your settings.

    0 讨论(0)
  • 2021-01-25 18:18

    Read Sending mail with MFMailComposeViewController

    First include MessageUI framework an implement MFMailComposeViewControllerDelegate.

    #import <MessageUI/MessageUI.h>
    #import <MessageUI/MFMailComposeViewController.h>
    
    @interface MainViewController : UIViewController <MFMailComposeViewControllerDelegate> {
    }
    

    then implement a method like this one an the delegate method for removing the mail controller.

    - (IBAction)pressentMailController:(id)sender {
    
        MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate = self;
    
        [picker setSubject:@"Test subject!"];
    
        // Set up the recipients.
        /*NSArray *toRecipients = [NSArray arrayWithObjects:@"first@example.com",
                                 nil];
        NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com",
                                 @"third@example.com", nil];
        NSArray *bccRecipients = [NSArray arrayWithObjects:@"four@example.com",
                                  nil];
    
        [picker setToRecipients:toRecipients];
        [picker setCcRecipients:ccRecipients];
        [picker setBccRecipients:bccRecipients];
        */
    
        // Attach an image to the email.
        /*NSString *path = [[NSBundle mainBundle] pathForResource:@"ipodnano"
                                                         ofType:@"png"];
        NSData *myData = [NSData dataWithContentsOfFile:path];
        [picker addAttachmentData:myData mimeType:@"image/png"
                         fileName:@"ipodnano"];
        */
        // Fill out the email body text.
        NSString *emailBody = @"It is raining in sunny California!";
        [picker setMessageBody:emailBody isHTML:NO];
    
        // Present the mail composition interface.
        [self presentModalViewController:picker animated:YES];
        [picker release]; // Can safely release the controller now.
    }
    
    // The mail compose view controller delegate method
    - (void)mailComposeController:(MFMailComposeViewController *)controller
              didFinishWithResult:(MFMailComposeResult)result
                            error:(NSError *)error
    {
        [self dismissModalViewControllerAnimated:YES];
    }
    

    The code allows to add recipients, body, subject and attachements. Uncomment the lines as needed.

    enter image description here

    0 讨论(0)
  • 2021-01-25 18:26

    Actual mail sending is not possible from the simulator. Install the APP on a phone to test that.

    However you can test everything that your APP can do/control/specify in the simulator. Everything after the pressing of the Send button is Apple, so you can assume that is working okay.

    0 讨论(0)
提交回复
热议问题