2013-05-10 185 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]; 
} 
相關問題