如何在iphone应用程序中附加电子邮件中的任何文档

时间:2013-05-18 05:05:11

标签: iphone objective-c ipad email

我正在从iphone应用程序发送电子邮件它工作正常,但我希望通过电子邮件我应该附加一个pdf文件,这是app.for测试的文档文件夹首先我从app的资源文件夹附加了一个png但它没有得到附件而不是通过电子邮件发送我正在使用以下代码。

  - (IBAction)onEmailResult

   {
if ([[MFMailComposeViewController class] canSendMail]) {
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    [picker setSubject:@"Pig Game"];


    [picker setToRecipients:toRecipients];

            int a=10;
    int b=100;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"project existing photo" ofType:@"png"];
    NSData *myData = [NSData dataWithContentsOfFile:path];
    [picker addAttachmentData:myData mimeType:@"png" fileName:@"icon.png"];

            NSString * emailBody = [NSString stringWithFormat:@"My Score %d",a];
    [picker setMessageBody:emailBody isHTML:NO];
            [self presentModalViewController:picker animated:YES];
            [picker release];
           }

else {


    int a=10;
    int b=20;
    NSString *recipients = @"mailto:imran_husain_2000@yahoo.com?&subject=Pig Game";
    NSString *body = [NSString stringWithFormat:@"&body=My Score: %d/%d, My Time: %@", a,b, time];

    NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
    email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
      }
     }

3 个答案:

答案 0 :(得分:1)

尝试以下代码段。

- (NSString *)pathForFile : (NSString *) fileName{
    return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: fileName];
}

- (void)sendMailWithAttachedFile:(NSString *) fileName extention:(NSString *) extension{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    //    NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:[self pathForResourse:fileName ofType:extension]];
    NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:[self pathForFile:[NSString stringWithFormat:@"%@.%@", fileName, extension]]];
    NSData *data=[[NSData alloc]initWithContentsOfURL:outputURL];
    [picker addAttachmentData:data mimeType:@"application/pdf" fileName:@"TestOne.pdf"];
    [self presentViewController:picker animated:YES completion:nil];
}

现在调用Send Mail方法为:

[self sendMailWithAttachedFile:@"TestOne" :@"pdf"];

答案 1 :(得分:0)

-(void)displayMailComposerSheet
{
 NSData *soundFile = [[NSData alloc] initWithContentsOfURL:YourDocumentFile];
    [mail addAttachmentData:soundFile mimeType:@".txt" fileName:@"YourDocumentFile.txt"];
}

这个代码实现在displayMailComposerSheet中我希望这段代码对你有用

答案 2 :(得分:0)

    MFMailComposeViewController *picker1 = [[MFMailComposeViewController alloc] init];
    NSArray *recipentsArray = [[NSArray alloc]initWithObjects:[shopDictValues objectForKey:@"vEmail"], nil];
    picker1.mailComposeDelegate = self;
    picker1.modalPresentationStyle = UIModalPresentationFormSheet;
    [picker1 setSubject:@"Subject"];
    [picker1 setToRecipients:recipentsArray];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSMutableArray *arydefault = [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults] valueForKey:@"jacketCount"]];
    for (int i=0;i<arydefault.count;i++)
    {
        NSString *pdfFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@.pdf",[[arydefault objectAtIndex:i]valueForKey:@"productlinename"],[arydefault objectAtIndex:i]]];
        [picker1 addAttachmentData:[NSData dataWithContentsOfFile:pdfFilePath] mimeType:@"application/pdf" fileName:[NSString stringWithFormat:@"Order - %@",[[[NSString stringWithFormat:@"%@%i",[[arr objectAtIndex:i]valueForKey:@"productlinename"],i] componentsSeparatedByCharactersInSet: [[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:@""]]];
    }
    NSString *emailBody = @"Email Body goes here.";
    [picker1 setMessageBody:emailBody isHTML:YES];
    [self presentModalViewController:picker1 animated:YES];