ios - 通过电子邮件从我的应用程序发送多个递增的 csvs

标签 ios objective-c

在过去的一周里,我一直在绞尽脑汁思考这个问题,现在还想不出该怎么办。

我目前有一个应用程序可以接收调查数据,并将其保存为 surveydata-mm-dd-yyyy 格式的 csv 文件。这通常适用于持续多天的事件,因此正常的事件周末将是

调查数据-09-13-2014 调查数据-09-14-2014 调查数据-09-15-2014

现在我希望参加这些事件的人能够简单地单击一个按钮,该按钮将在电子邮件中添加所有存储在应用程序文档文件夹中的文件。

除了能够告诉应用程序查找具有这些名称的那些文件并将它们包含在电子邮件中之外,我几乎已经完成了所有设置和功能。

这是我的代码

- (IBAction)emailButton:(id)sender {

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MM-dd-yyyy"];

    NSString *dateString = [dateFormat stringFromDate:[NSDate date]];
    NSString *path = [[self applicationDocumentsDirectory].path
                      stringByAppendingPathComponent:[NSString stringWithFormat:@"_SurveyData_%@.csv",dateString]];

  /*  if(![[NSFileManager defaultManager] fileExistsAtPath:path]) {*/
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
        mailer.mailComposeDelegate = self;
        [mailer setSubject:@"CSV File"];
        [mailer setToRecipients:toRecipents];
        [mailer addAttachmentData:[NSData dataWithContentsOfFile:@"_SurveyData_%@.csv"]
                         mimeType:@"text/csv"
                         fileName:@"FileName"];
        [self presentModalViewController:mailer animated:YES];

    //}

如果有人可以帮助我,因为我觉得我已经很接近了,我只是在等待它全部点击并变得有意义。

如果我遗漏了什么或者我的代码太模糊,请告诉我。

这是我用来将数据写入 CSV 的代码,以便更好地了解它是如何发生的。

-(void)writeSurveyDataToCSV:(NSString *)text {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MM-dd-yyyy"];

    NSString *dateString = [dateFormat stringFromDate:[NSDate date]];
    NSString *path = [[self applicationDocumentsDirectory].path
                      stringByAppendingPathComponent:[NSString stringWithFormat:@"_SurveyData_%@.csv",dateString]];
    if(![[NSFileManager defaultManager] fileExistsAtPath:path]) {

        NSString *header = @" gender,age,zip code,duration(sec), own a bike?,response1,response2,response3,response4,response5,response6, response7, response8, response9\n";
        [header writeToFile:path atomically:YES
                 encoding:NSUTF8StringEncoding error:nil];

    }
    text = [text stringByAppendingString:@"\n"];
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
    [fileHandle seekToEndOfFile];
    [fileHandle writeData:[text dataUsingEncoding:NSUTF8StringEncoding]];

}

编辑:感谢 Danh 的指导,这是我的解决方案

- (IBAction)emailButton:(id)sender {

    NSArray *toRecipents = [NSArray arrayWithObject:@"reflex@ilovetheory.com"];

    MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
    mailer.mailComposeDelegate = self;
    [mailer setSubject:@"CSV File"];
    [mailer setToRecipients:toRecipents];

    NSArray *filenames = [self filesNamesStartingAt:[NSDate date] count:165];
    [self attachFilesNamed:filenames toMailer:mailer];

    [self presentModalViewController:mailer animated:YES];

}

// answer count strings, named for days starting at date and the count-1 following days
- (NSArray *)filesNamesStartingAt:(NSDate *)date count:(NSInteger)count {

    NSMutableArray *result = [NSMutableArray array];
    static NSDateFormatter *dateFormat;

    if (!dateFormat) {
        dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"MM-dd-yyyy"];
    }

    for (int i=0; i<count; ++i) {
        NSString *dateString = [dateFormat stringFromDate:date];
        NSString *path = [[self applicationDocumentsDirectory].path
                          stringByAppendingPathComponent:[NSString stringWithFormat:@"_SurveyData_%@.csv",dateString]];
        [result addObject:path];
        date = [self addDayToDate:date];
    }
    for (int i=0; i<count; ++i) {
        NSString *dateString = [dateFormat stringFromDate:date];
        NSString *path = [[self applicationDocumentsDirectory].path
                          stringByAppendingPathComponent:[NSString stringWithFormat:@"_SurveyData_%@.csv",dateString]];
        [result addObject:path];
        date = [self subDayToDate:date];
    }
    return result;
}

// answer a new date, advanced one day from the passed date
- (NSDate *)addDayToDate:(NSDate *)date {

    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:1];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    return [gregorian dateByAddingComponents:components toDate:date options:0];
}

- (NSDate *)subDayToDate:(NSDate *)date {

    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:-1];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    return [gregorian dateByAddingComponents:components toDate:date options:0];
}

- (void)attachFilesNamed:(NSArray *)paths toMailer:(MFMailComposeViewController *)mailer {

    for (NSString *path in paths) {
        if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
            NSData *data = [NSData dataWithContentsOfFile:path];
            [mailer addAttachmentData:data mimeType:@"text/csv" fileName:path];
        } else {
            NSLog(@"warning, no file at path %@", path);
        }
    }
}

- (NSURL *)applicationDocumentsDirectory {
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
                                                   inDomains:NSUserDomainMask] lastObject];
}

最佳答案

看起来你确实很亲近。也许需要进一步分解问题:

从一个方法开始,该方法将创建从给定日期开始的几天的文件名...

// answer count strings, named for days starting at date and the count-1 following days
- (NSArray *)filesNamesStartingAt:(NSDate *)date count:(NSInteger)count {

    NSMutableArray *result = [NSMutableArray array];
    static NSDateFormatter *dateFormat;

    if (!dateFormat) {
        dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"MM-dd-yyyy"];
    }

    for (int i=0; i<count; ++i) {
        NSString *dateString = [dateFormat stringFromDate:date];
        NSString *path = [[self applicationDocumentsDirectory].path
                      stringByAppendingPathComponent:[NSString stringWithFormat:@"_SurveyData_%@.csv",dateString]];
        [result addObject:path];
        date = [self addDayToDate:date];
    }
    return result;
}

// answer a new date, advanced one day from the passed date
- (NSDate *)addDayToDate:(NSDate *)date {

    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:1];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    return [gregorian dateByAddingComponents:components toDate:date options:0];
}

现在,添加一个将一组命名文件附加到邮件 Controller 的方法:

- (void)attachFilesNamed:(NSArray *)paths toMailer:(MFMailComposeViewController *)mailer {
    for (NSString *path in paths) {
        if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
            NSData *data = [NSData dataWithContentsOfFile:path];
            [mailer addAttachmentData:data mimeType:@"text/csv" fileName:path];
        } else {
            NSLog(@"warning, no file at path %@", path);
        }
    }
}

其余的实际上是自己写的(我希望)...

MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:@"CSV File"];
[mailer setToRecipients:toRecipents];

NSArray *filenames = [self fileNamesStartingAt:[NSDate date] count:3];
[self attachFilesNamed:filenames toMailer:mailer];

请注意,如所写,这将使用今天和接下来两天的文件名。如果这不是您的要求,您可以调整 addDay 方法以创建一个减去天数的方法,然后串联使用这些方法。

关于ios - 通过电子邮件从我的应用程序发送多个递增的 csvs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25633685/

相关文章:

objective-c - 如何使用 objective-c 在纸牌游戏中画出完美的曲线?

ios - 如何测量 Table View 滚动的速度?

iphone - 推送通知在 iOS 5.0.1 上不起作用

ios - 隐藏选项卡的基于导航的应用程序

objective-c - 使用 Objective C 访问 Sprite in Touch 事件

ios - 如何修复 "malloc: can' t 保护区域的 poSTLude 保护页面“iOS 上的警告

ios - 在 NSString IOS 中发送\t

iphone - 从左向右翻转iOS 4之前的UIView

iOS 协议(protocol)/委托(delegate)混淆?

objective-c - UIView clipToBounds 不会阻止 subview 在父 View 之外接收触摸