0

アプリで .csv ファイルを添付または作成する際に問題が発生しています。どこに問題があるのか​​わかりません。電子メール ビューでは .csv 添付ファイルが表示されますが、電子メールを受信すると添付ファイルがありません。オブジェクトの配列 (dataController.masterList) を CHCSVWriter に送信します。今週、メールの添付ファイルと CHCSVWriter に関する他の質問の解決策を試すのに多くの時間を費やしましたが、明らかに解決策はどれも機能していません。問題はどこにあり、何を提案しますか? よろしくお願いします、ハッピーデイズ、-ロブ

- (IBAction)send:(id)sender {
static NSDateFormatter *formatter = nil;
if (formatter == nil) {
    formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
}

NSString *filepath = @"testfile.csv";
filepath = [filepath stringByExpandingTildeInPath];

NSOutputStream *exportStream = [NSOutputStream outputStreamToFileAtPath:filepath append:NO];
NSStringEncoding encodingA = NSUTF8StringEncoding;

CHCSVWriter *csvWriter = [[CHCSVWriter alloc] initWithOutputStream:exportStream encoding:encodingA delimiter:','];
[csvWriter writeField:[NSString stringWithFormat:@"One"]];
[csvWriter writeLineOfFields:dataController.masterList];
[csvWriter closeStream];
NSString *path = [[NSBundle mainBundle] pathForResource:filepath ofType:@".csv"];
NSData *mydata = [NSData dataWithContentsOfFile:path];

if ([MFMailComposeViewController canSendMail]) {
    MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
    [mail setMailComposeDelegate:self];
    [mail setSubject:@"CSV File"];
    [mail addAttachmentData:mydata mimeType:@"text/csv" fileName:filepath];
    [mail setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentViewController:mail animated:YES completion:nil];
    }
}

- (void)mailComposeController:(MFMailComposeViewController *)controller
      didFinishWithResult:(MFMailComposeResult)result
                    error:(NSError *)error {

[self dismissViewControllerAnimated:YES completion:nil];
4

2 に答える 2

0

私はそれを理解し、CHCSVライターの使用をやめ、配列を書き、配列のコンポーネントを「、」で結合しました。

- (IBAction)send:(id)sender {
static NSDateFormatter *formatter = nil;
if (formatter == nil) {
    formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
}

NSIndexPath *index2 = 0;
NSUInteger i = 0;
NSString *holder;
NSArray *holderArray;
NSArray *saverArray;

while (i < dataController.countOfList) {

TimeSheetEntry *sAtIndex = [self.dataController objectInListAtIndex:index2.row];

    NSString *dayhold = [formatter stringFromDate:sAtIndex.date];
    holderArray = [[NSArray alloc] initWithObjects:sAtIndex.name, sAtIndex.jobnum, sAtIndex.hours, sAtIndex.jobnotes, dayhold, nil];
    saverArray = [saverArray arrayByAddingObjectsFromArray:holderArray];
    i++;
    NSIndexPath *index3 = [NSIndexPath indexPathForRow:i inSection:1];
    index2 = index3;
}

holder = [saverArray componentsJoinedByString:@","];//this is the seperating variable

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
NSString *outputFile = [docDirectory stringByAppendingPathComponent:@"timesheet.csv"];
NSError *csvError = NULL;

BOOL written = [holder writeToFile:outputFile atomically:YES encoding:NSUTF8StringEncoding error:&csvError];

if (!written)
    NSLog(@"write failed, error=%@", csvError);


if ([MFMailComposeViewController canSendMail]) {
    MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
    [mail setMailComposeDelegate:self];
    [mail setSubject:@"CSV File"];
    //[mail setMessageBody:holder isHTML:YES];
    [mail addAttachmentData:[NSData dataWithContentsOfFile:outputFile] mimeType:@"text/csv" fileName:@"timesheet.csv"];
    [mail setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentViewController:mail animated:YES completion:nil];
    }
}

- (void)mailComposeController:(MFMailComposeViewController *)controller
      didFinishWithResult:(MFMailComposeResult)result
                    error:(NSError *)error {

[self dismissViewControllerAnimated:YES completion:nil];
}


@end
于 2013-08-14T18:20:02.157 に答える
0

まだ CHCSVParser を使用したい人のために、ファイル パスは絶対パスでなければならないことがわかりました。

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Task"
                                          inManagedObjectContext:_tempManagedObjectContext];
[fetchRequest setEntity:entity];
NSError *error;
NSArray *fetchedObjects = [_tempManagedObjectContext executeFetchRequest:fetchRequest error:&error];
NSURL *datapath = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"export.csv"];

NSOutputStream *output = [NSOutputStream outputStreamToMemory];
CHCSVWriter *writer = [[CHCSVWriter alloc] initWithOutputStream:output encoding:NSUTF8StringEncoding delimiter:','];

// Fetch objects to write to .csv
for (Task *task in fetchedObjects) {
    [writer writeLineOfFields:@[task.taskID, task.taskTitle, task.taskDescription]];
}

entity = [NSEntityDescription entityForName:@"Journal"
                                          inManagedObjectContext:_tempManagedObjectContext];
[fetchRequest setEntity:entity];
fetchedObjects = [_tempManagedObjectContext executeFetchRequest:fetchRequest error:&error];

for (JournalEntry *entry in fetchedObjects) {
    [writer writeLineOfFields:@[entry.day, entry.entryTitle, entry.entryDescription]];
}

[writer closeStream];

NSData *buffer = [output propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
[buffer writeToURL:datapath atomically:NO];
于 2014-10-07T15:24:40.637 に答える