簡體   English   中英

NSFileManager createFileAtPath在iPad上失敗但在Simulator上運行

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

我正在嘗試為我的應用程序的每次執行編寫一個簡單的日志文件。 日志文件名基於當前時間並存儲在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

我哪里錯了?

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

您正在嘗試在應用程序包內創建一個文件,該文件在設備上是只讀的。 將您的文件放入應用程序的Documents或Library目錄中。

您正在嘗試在無權創建文件的位置創建文件,

在文檔文件夾中創建文件,

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

我通過更改我的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