简体   繁体   English

NSFileManager createFileAtPath在iPad上失败但在Simulator上运行

[英]NSFileManager createFileAtPath failing on iPad but works on Simulator

I am trying to write a simple log file for each execution of my app. 我正在尝试为我的应用程序的每次执行编写一个简单的日志文件。 The logfile name is based on the current time and is stored in the app directory. 日志文件名基于当前时间并存储在app目录中。 The code below is what I am trying which works on the simulator but when I execute on an iPad it fails with 'Operation not permitted' (EPERM in errno.h). 下面的代码是我正在尝试在模拟器上工作的但是当我在iPad上执行时它失败并且“不允许操作”(errno.h中的EPERM)。

-(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;
}

I tried the commented out code too but that also fails. 我也尝试了注释掉的代码,但也失败了。 I am new to iOS programming so might be missing something basic. 我是iOS编程的新手,所以可能会遗漏一些基本的东西。 Anyway the output form the above shows the following on iPad: 无论如何,上面的输出形式在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

but on the simulator it works: 但在模拟器上它的工作原理:

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

Where am I going wrong? 我哪里错了?

--edit Here is the code for the GetLogFileNameForTimeNow --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]];
}

and the date formatter is setup as follows in the constructor of my class: 并且我的类的构造函数中的日期格式化程序设置如下:

mDateFormatter = [[NSDateFormatter alloc] init];
NSString* DateFormat = @"yyyyMMddHHmmss";
[mDateFormatter setDateFormat:DateFormat];

You are trying to create a file inside app bundle, which is read-only on the device. 您正在尝试在应用程序包内创建一个文件,该文件在设备上是只读的。 Put your files into Documents or Library directories of the application. 将您的文件放入应用程序的Documents或Library目录中。

You are trying to create a file in a place where you don't have permissions to create files, 您正在尝试在无权创建文件的位置创建文件,

Create your files inside documents folder, 在文档文件夹中创建文件,

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];

I have fixed the problem by changing my GetLogFileNameForTimeNow function as follows: 我通过更改我的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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM