2013-05-10 217 views
1

我正在尝试为每次执行我的应用程序编写一个简单的日志文件。日志文件名称基于当前时间并存储在应用程序目录中。下面的代码是我尝试在模拟器上工作的,但是当我在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]; 
+0

显示你的'GetLogFileNameForTimeNow'代码。 – rmaddy 2013-05-10 14:19:59

+0

这是保存到应用程序文件夹,我将在这里添加,但代码不允许在注释中。它现在可以通过保存到文档文件夹 – allanmb 2013-05-10 15:50:36

+0

注释中允许使用代码,但正确的做法是编辑原始问题并将其他代码添加为更新。 – rmaddy 2013-05-10 16:13:32

回答

12

您正在试图建立一个应用程序包内的文件,在设备上是只读的。将您的文件放入应用程序的Documents或Library目录中。

+0

谢谢你的回复。我想我有权写入应用程序包目录,但我猜不是! – allanmb 2013-05-10 15:32:25

8

您要创建在您没有权限创建文件的地方文件,

的文档文件夹中创建文件,

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"20130510150714.log"]; 
BOOL Success = [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil]; 
+0

谢谢,我会试试这个。 – allanmb 2013-05-10 15:32:47

+0

是的,这是一种魅力,谢谢! – allanmb 2013-05-10 15:50:52

1

我有固定的问题通过改变我的GetLogFileNameForTimeNow功能如下:

-(NSString*)GetLogFileNameForTimeNow 
{ 
    NSString* FileName = [NSString stringWithFormat:@"%@.log", [self GetTimeAsString] ]; 

    NSArray* Paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString* DocumentsDirectory = [Paths objectAtIndex:0]; // Get documents folder 
    return [NSString stringWithFormat:@"%@/%@", DocumentsDirectory, FileName]; 
} 
相关问题