0

我正在从 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]];
      }
     }
4

3 回答 3

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

现在调用发送邮件方法为:

[self sendMailWithAttachedFile:@"TestOne" :@"pdf"];
于 2013-05-18T05:23:18.757 回答
0
-(void)displayMailComposerSheet
{
 NSData *soundFile = [[NSData alloc] initWithContentsOfURL:YourDocumentFile];
    [mail addAttachmentData:soundFile mimeType:@".txt" fileName:@"YourDocumentFile.txt"];
}

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

于 2013-05-18T05:11:12.097 回答
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];
于 2013-05-18T05:15:29.973 回答