在iPhone应用程序中嵌入邮件撰写

时间:2009-11-22 21:50:03

标签: iphone email embed

我希望我的应用发送邮件。我可以使用mailto:URL方案,但它会在启动iPhone邮件时终止我的应用程序。 The Independent(英国报纸)的新闻阅读器似乎在应用程序中调出了一个邮件撰写视图。当您发送或取消时,应用程序会立即重新出现。

有谁知道怎么做?

谢谢,

2 个答案:

答案 0 :(得分:3)

您需要使用3.0 Message UI Framework!

答案 1 :(得分:0)

#import <MessageUI/MessageUI.h>

@interface ViewReminderViewController_iPhone : UIViewController
            <MFMailComposeViewControllerDelegate>
{
UiButton *mailButton;
}
- (IBAction)EmailButton:(id)sender;
@end

@implementation ViewController
- (IBAction)EmailButton:(id)sender
{

    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    [picker setSubject:@"Your EMail Subject"];

    //SET UP THE RECIPIENTS (or leave not set)
    //NSArray *toRecipients = [NSArray arrayWithObjects:@"first@example.com", nil];
    //[picker setToRecipients:toRecipients];

    //NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
    //[picker setCcRecipients:ccRecipients];

    //NSArray *bccRecipients = [NSArray arrayWithObjects:@"four@example.com", nil];
    //[picker setBccRecipients:bccRecipients];

    //ATTACH FILE

    NSString *path;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"MediaFiles"];
    path = [path stringByAppendingPathComponent:MyFileName];

    NSLog(@"Attaching file: %@", path);

    if ([[NSFileManager defaultManager] fileExistsAtPath:path])     //Does file exist?
    {
            NSLog(@"File exists to attach");

            NSData *myData = [NSData dataWithContentsOfFile:path];

            [picker addAttachmentData:myData mimeType:@"application/octet-stream"
                             fileName:@"DesredFileName.mov"];

    }

    //CREATE EMAIL BODY TEXT
    NSString *emailBody = @"Your Email Body";
    [picker setMessageBody:emailBody isHTML:NO];

    //PRESENT THE MAIL COMPOSITION INTERFACE
    [self presentModalViewController:picker animated:YES];
    [picker release];

}
Delegate To Clear Compose Email View Controller


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

    [self dismissModalViewControllerAnimated:YES];      //Clear the compose email view controller
}