我有以下问题:
我正在尝试将文件附加到邮件中,并通过我的iPad应用程序中的邮件表单发送。我认为一切顺利,日志输出说邮件已发送,但我没有收到。
我尝试了一切,但仍然无法解决问题。
这是我的代码,请查看它并告诉我是否有任何奇怪或错误:
-(void)createCSVFile
{
NSMutableString *csv = [NSMutableString stringWithString:@"UniqueID,playerNumber,DateAndHour,Prize"];
NSUInteger counter = [playerNumber count];
NSLog (@"%d", counter);
// provided all arrays are of the same length
for (NSUInteger i=0; i < counter; i++ )
{
[csv appendFormat:@"\n%@,%d,%@,%@",
[uniqueID objectAtIndex:i],
[[playerNumber objectAtIndex:i] intValue],
[dateAndHour objectAtIndex:i],
[prizeWon objectAtIndex:i]
];
// NSLog (@"%@, %d, %@, %@", [uniqueID objectAtIndex:i], [[playerNumber objectAtIndex:i]intValue], [dateAndHour objectAtIndex:i], [prizeWon objectAtIndex:i]);
// instead of integerValue may be used intValue or other, it depends how array was created
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
filePath = [NSString stringWithFormat:@"%@/VictoryStats.csv", docDirectory];
NSString *outputFileName = [docDirectory stringByAppendingPathComponent:@"VictoryStats.csv"];
NSError *error;
BOOL res = [csv writeToFile:outputFileName atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (res)
{
NSLog (@"created");
}
if (!res) {
NSLog(@"Error %@ while writing to file %@", [error localizedDescription], outputFileName );
}
}
-(void)sendMail
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:@"CSV File"];
[mailer addAttachmentData:[NSData dataWithContentsOfFile:filePath]
mimeType:@"text/csv"
fileName:@"VictoryStats.csv"];
[self presentViewController:mailer animated:YES completion:NULL];
}
我认为我的代码很好,但似乎不是。
请看一下。
提前致谢。
答案 0 :(得分:0)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *filename = [docDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Product.csv"]];
NSError *error = NULL;
BOOL written = [csvstr writeToFile:filename atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (!written)
NSLog(@"write failed, error=%@", error);
//CSV File
NSData *myData = [NSData dataWithContentsOfFile:[docDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Product.csv"]]];
[mail addAttachmentData:myData mimeType:@"text/csv" fileName:[NSString stringWithFormat:@"Product.csv"]];