我正在生成PDF文件并将其保存到我的设备,然后再从我的应用程序发送电子邮件。目前它没有手动或自动清除,所以我在看NSTemporaryDirectory。我查看了各种网站,也在这里查找我的具体查询的答案,但找不到它。
我有以下功能:
+(NSString *)getTempFilePathName {
NSString *tempFilePathName = nil;
NSString *tempFileTemplate = [NSTemporaryDirectory() stringByAppendingPathComponent:@"tempfile.XXXXXX"];
const char *tempFileTemplateCString = [tempFileTemplate fileSystemRepresentation];
char *tempFileNameCString = (char *)malloc(strlen(tempFileTemplateCString) + 1);
strcpy(tempFileNameCString, tempFileTemplateCString);
int fileDescriptor = mkstemp(tempFileNameCString);
if (fileDescriptor != -1) {
// File opened successfully
tempFilePathName = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:tempFileNameCString length:strlen(tempFileNameCString)];
close(fileDescriptor);
}
free(tempFileNameCString);
return tempFilePathName;
}
这会返回一个NSString,其中包含一个temp文件的完整路径和文件名,文件名的'XXXXXX'组件以字母和数字的唯一组合替换。当我使用它来保存PDF文件时,我需要使用扩展名“.PDF”修复“XXXXXX”位。我最好还指定文件名本身,例如“Order-123.PDF”。
我可以编辑我的方法来传递文件名并在stringByAppendingPathComponent参数中使用它吗?每次调用此方法时,目录名是否都是唯一的?
答案 0 :(得分:1)
我自己解决了这个问题。在调用上述例程的行之后我做了:
NSString *newFilePath = [tempNameStub stringByAppendingPathExtension:@"pdf"];
将.pdf
添加到最后。