如何在模拟器中测试MFMailComposeViewController

时间:2011-06-22 12:36:14

标签: objective-c xcode

有没有办法在iphone模拟器中测试从MFMailComposeViewController发送电子邮件?

3 个答案:

答案 0 :(得分:4)

不,你不能在模拟器上测试它...(我的意思是你的邮件不会被发送)。我们将能够测试有限的东西,例如:视图将如何,当用户点击时会发生什么取消按钮等...

要测试邮件是否已发送,您必须使用设备。应在您的设置中为设备配置一些邮件(例如:gmail)。

答案 1 :(得分:1)

无法从模拟器发送实际邮件。 在手机上安装APP进行测试。

但是,您可以测试APP在模拟器中可以执行/控制/指定的所有内容。 按下发送按钮后的所有内容都是Apple,因此您可以认为是 工作还可以。

答案 2 :(得分:-4)

阅读Sending mail with MFMailComposeViewController

首先包括MessageUI框架一个工具MFMailComposeViewControllerDelegate。

#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>

@interface MainViewController : UIViewController <MFMailComposeViewControllerDelegate> {
}

然后实现像这样的方法,删除邮件控制器的委托方法。

- (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];
}

该代码允许添加收件人,正文,主题和附件。根据需要取消注释行。

enter image description here