2013-05-18 48 views
0

我從iPhone應用程序發送電子郵件它工作正常,但我想,用電子郵件,我應該附加一個PDF文件這是app.for測試的文件夾首先我連着PNG從應用程序的資源文件夾,但它不會被附加,並不發送電子郵件我使用下面的代碼。如何附加任何文件在電子郵件中的iPhone應用程序

- (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:[email protected]?&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]]; 
     } 
    } 
+0

爲什麼downvote我寫正確的代碼,如果在現在在我的身邊工作的任何東西,所以它並不意味着downvote? –

+0

MIMIE類型看起來不太好。嘗試將其更改爲「image/png」。 –

+0

嘗試這兩個連接 http://stackoverflow.com/questions/2486705/storing-image-in-plist http://stackoverflow.com/questions/10846620/ios-load-image- from-plist-file –

回答

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"]; 
+0

kFileNameEmailReport這是什麼和什麼給在文件擴展名在你的代碼 –

+0

我已經添加了我的代碼在你的編輯看到它的確顯示電子郵件TestOne.pdf,但它不是在電子郵件 –

+0

附我用你的代碼不是我的我已在您的代碼 –

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

在displayMailComposerSheet此代碼實現,我希望這個代碼是對你有用

+0

地方使用此代碼你可以編輯我的代碼 –

+1

.text?闕!? ... –

+0

顯示您的問題,我在你的代碼編輯 –

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]; 
+0

這裏我附上PDF的數量。所以,我已經打好了它。 –

+0

productlinename這是我可以在哪裏給我的pdf名稱 –

+0

是的[NSString stringWithFormat:@「%@。pdf」,yourFileName]; –

相關問題