我正在嘗試爲每次執行我的應用程序編寫一個簡單的日誌文件。日誌文件名稱基於當前時間並存儲在應用程序目錄中。下面的代碼是我嘗試在模擬器上工作的,但是當我在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