【问题标题】:how to attach any document in email in iphone app如何在 iPhone 应用程序的电子邮件中附加任何文档
【发布时间】:2013-05-13 07:42:32
【问题描述】:

我正在从 iphone 应用程序发送电子邮件,它工作正常,但我希望通过电子邮件附加一个 pdf 文件,该文件是应用程序的文档文件夹。为了测试,我首先从应用程序的资源文件夹中附加了一个 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]];
      }
     }

【问题讨论】:

  • 如果我写了正确的代码,为什么我写了正确的代码,所以这并不意味着要投反对票?
  • MIMIE 类型看起来不太好。尝试将其更改为“image/png”。
  • 我的猜测是您投了反对票,因为您提出的问题可以通过运行搜索轻松回答。当然,他或她应该说明原因。
  • @TBlue 我从我这边改变了 mime 类型它现在可以工作了,谢谢你的评论

标签: iphone objective-c ipad email


【解决方案1】:

试试下面的代码 sn-p。

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

【讨论】:

  • kFileNameEmailReport 这是什么以及在代码中的文件扩展名中给出什么
  • 我在您的编辑中添加了我的代码,看到它确实在电子邮件中显示了 TestOne.pdf,但它没有附加在电子邮件中
  • 我从应用程序的文档文件夹中获取的文件和文件名是 TestOne 如何给出该路径
  • 未能找到 PDF 标题:找不到“%PDF”。这给出了错误
  • 我想我需要发布另一个关于如何将文件从文档目录附加到 iPhone 应用程序中的电子邮件的问题
【解决方案2】:
-(void)displayMailComposerSheet
{
 NSData *soundFile = [[NSData alloc] initWithContentsOfURL:YourDocumentFile];
    [mail addAttachmentData:soundFile mimeType:@".txt" fileName:@"YourDocumentFile.txt"];
}

此代码在 displayMailComposerSheet 中实现,我希望此代码对您有用

【讨论】:

【解决方案3】:
    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];

【讨论】:

  • 这里我附上了 PDF 的数量。所以,我已经循环了。
  • productlinename 这是我可以提供我的 pdf 名称的地方
  • 这里是 [NSString stringWithFormat:@"%@.pdf",yourFileName];
猜你喜欢
  • 2012-01-27
  • 1970-01-01
  • 1970-01-01
  • 2013-05-13
  • 1970-01-01
  • 1970-01-01
  • 2020-01-20
  • 2015-09-07
  • 2016-09-08
相关资源
最近更新 更多