how to test MFMailComposeViewController in simulat

2020-04-21 01:03发布

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

3条回答
Melony?
2楼-- · 2020-04-21 01:42

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.

查看更多
▲ chillily
3楼-- · 2020-04-21 01:44

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.

查看更多
聊天终结者
4楼-- · 2020-04-21 02:04

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

查看更多
登录 后发表回答