2013-10-31 3 views
0

내 응용 프로그램에서 .csv 파일을 첨부 파일로 보낼 수 있지만 해당 첨부 파일의 이름 파일을 줄이려고합니다. csv 파일 더미가 받는 사람. 코드의iOS : 전자 메일 첨부 파일 이름

조각 :

<_var_mobile_Applications_BE8CE610-A83E-4C79-8B9C-0263FA6881D6_Documents_Tramo2Control4.csv> 

그리고 난 당신을 그냥 "Tramo2Control4.csv"

싶습니다 수있는 것 :

...  
if (dorsalesPorTramoYcontrol && dorsalesPorTramoYcontrol.count) 
       { 
        NSMutableString *mainString = [[ NSMutableString alloc]initWithString:@"dorsal,paso,tiempo\n"]; 

        for (NSManagedObject *get in dorsalesPorTramoYcontrol) { 

         //dorsales 
         NSString *string =[get valueForKey:@"dorsal"]; 
         [mainString appendFormat:@"%@,",string]; 

         //paso 
         string = [get valueForKey:@"paso"]; 
         string=[string stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""]; 
         [mainString appendFormat:@"%@,",string]; 

         //tiempo 
         string = [get valueForKey:@"tiempo"]; 
         string=[string stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""]; 
         [mainString appendFormat:@"%@",string]; 

           [mainString appendFormat:@"\n"]; 
         } 

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *documentsDirectoryPath = [paths objectAtIndex:0]; 

file = [NSString stringWithFormat:@"%@/Tramo%@Control%@.csv", documentsDirectoryPath,section,control]; 

        NSError *csVerror= NULL; 
        BOOL written = [mainString writeToFile:file atomically:YES encoding:NSUTF8StringEncoding error:&csVerror]; 
        if (!written) { 
         NSLog(@"Writing failed, error = %@",csVerror); 
        }else { 
         NSLog(@"Data saved! File path = %@",file); 
         [self composeEmail]; 
        } 
       } 
    } 

    -(void)composeEmail{ 

     if ([MFMailComposeViewController canSendMail]) 
     { 

      MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init]; 
      mailer.mailComposeDelegate = self; 
      [mailer setSubject:[NSString stringWithFormat:@"Resultados Tramo: %@ - Control: %@", section, control]]; 
      NSArray *toRecipients = [NSArray arrayWithObjects:@"[email protected]", nil]; 
      [mailer setToRecipients:toRecipients]; 

      // Logo 
      UIImage *myImage = [UIImage imageNamed:@"logo.png"]; 
      NSData *imageData = UIImagePNGRepresentation(myImage); 
      [mailer addAttachmentData:imageData mimeType:@"image/png" fileName:@"Icon"]; 


      [mailer addAttachmentData:[NSData dataWithContentsOfFile:file] mimeType:@"text/csv" fileName:file]; 
      NSString *emailBody = 
      [NSString stringWithFormat:@"Resultados Tramo: %@ - Control: %@ \nDorsal - Paso - Tiempo", section, control]; 
      [mailer setMessageBody:emailBody isHTML:NO]; 
      mailer.modalPresentationStyle = UIModalPresentationPageSheet; 
      [self presentViewController:mailer animated:YES completion:nil]; 
     } 
     else 
     { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure" 
                  message:@"Your device doesn't support the composer sheet" 
                  delegate:nil 
                cancelButtonTitle:@"OK" 
                otherButtonTitles: nil]; 
      [alert show]; 
     } 
    } 

파일 이름은 다음과 같다 그것을 얻으려면 몇 가지 제안을 해주시겠습니까?

답변

0

변경 다음 줄

[mailer addAttachmentData:[NSData dataWithContentsOfFile:file] mimeType:@"text/csv" fileName:file]; 

사람 :

NSString *fileName = [file lastPathComponent]; 
[mailer addAttachmentData:[NSData dataWithContentsOfFile:file] mimeType:@"text/csv" fileName:fileName]; 

당신은 원래 파일 이름을 설정하고 전체 파일 경로 여야합니다.

관련 문제