我正在尝试为每次执行我的应用程序编写一个简单的日志文件。日志文件名称基于当前时间并存储在应用程序目录中。下面的代码是我尝试在模拟器上工作的,但是当我在iPad上执行时,它会以'Operation not permitted'操作失败(errno.h中的EPERM)。NSFileManager createFileAtPath在iPad上失败,但在模拟器上工作
-(BOOL)CreateLogFile
{
mLogFilePath = [self GetLogFileNameForTimeNow];
BOOL Success = [[NSFileManager defaultManager] createFileAtPath:mLogFilePath contents:nil attributes:nil];
/*BOOL Success = [[NSFileManager defaultManager] createFileAtPath:mLogFilePath contents:nil attributes:[NSDictionary dictionaryWithObject:NSFileProtectionComplete
forKey:NSFileProtectionKey]];*/
if (Success)
{
NSLog(@"Successfully created file: %@", mLogFilePath);
mLogFileHandle = [NSFileHandle fileHandleForWritingAtPath:mLogFilePath];
[self WriteInstanceInformationToLog];
}
else
{
NSLog(@"Failed to create file: %@ %s", mLogFilePath, strerror(errno));
}
return Success;
}
我试过了注释掉的代码,但那也失败了。我是iOS编程的新手,所以可能会缺少一些基本的东西。反正输出形式上面显示在iPad上执行以下操作:
2013-05-10 14:54:51.794 RichTest1[128:907] Failed to create file:
/var/mobile/Applications/XXXX/RichTest1.app/20130510145451.log Operation not permitted
但它的工作原理模拟器:
2013-05-10 15:07:14.696 RichTest1[1604:c07] Successfully created file:
Users/abruce/Library/Application Support/iPhone Simulator/6.1/Applications/XXXX/RichTest1.app/20130510150714.log
我要去哪里错了?
--edit 下面是GetLogFileNameForTimeNow
-(NSString*)GetLogFileNameForTimeNow
{
NSString* FileName = [NSString stringWithFormat:@"%@.log", [self GetTimeAsString] ];
NSString* AppFolderPath = [[NSBundle mainBundle] resourcePath];
return [NSString stringWithFormat:@"%@/%@", AppFolderPath, FileName];
}
-(NSString*)GetTimeAsString
{
return [mDateFormatter stringFromDate:[NSDate date]];
}
代码和日期格式是建立在我的类的构造函数如下:
mDateFormatter = [[NSDateFormatter alloc] init];
NSString* DateFormat = @"yyyyMMddHHmmss";
[mDateFormatter setDateFormat:DateFormat];
显示你的'GetLogFileNameForTimeNow'代码。 – rmaddy 2013-05-10 14:19:59
这是保存到应用程序文件夹,我将在这里添加,但代码不允许在注释中。它现在可以通过保存到文档文件夹 – allanmb 2013-05-10 15:50:36
注释中允许使用代码,但正确的做法是编辑原始问题并将其他代码添加为更新。 – rmaddy 2013-05-10 16:13:32