gpt4 book ai didi

ios - NSFileManager createFileAtPath 在 iPad 上失败但在模拟器上工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:49:08 24 4
gpt4 key购买 nike

我正在尝试为我的应用程序的每次执行编写一个简单的日志文件。日志文件名以当前时间为准,保存在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];

最佳答案

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

关于ios - NSFileManager createFileAtPath 在 iPad 上失败但在模拟器上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16484314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com