【问题标题】:NSFileManager createFileAtPath failing on iPad but works on SimulatorNSFileManager createFileAtPath 在 iPad 上失败但在模拟器上工作
【发布时间】:2013-05-05 06:13:29
【问题描述】:

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

我也尝试了注释掉的代码,但也失败了。我是 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

我哪里错了?

--编辑 这是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的代码。
  • 它正在保存到应用程序文件夹中,我将在此处添加,但 cmets 中不允许使用代码。它现在通过保存到文档文件夹来工作
  • cmets 中允许使用代码,但正确的做法是编辑您的原始问题并添加附加代码作为更新。
  • 谢谢,我已经更新了原来的问题

标签: ios objective-c nsfilemanager createfile


【解决方案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];
}

【讨论】:

    【解决方案2】:

    您正在尝试在您无权创建文件的地方创建文件,

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

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

    【讨论】:

    • 谢谢,我试试看。
    【解决方案3】:

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

    【讨论】:

    • 感谢您的回复。我以为我有权写入 app bundle 目录,但我想没有!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 2016-02-25
    • 1970-01-01
    相关资源
    最近更新 更多