2012-11-26 91 views
2

我无法使NSFileManager方法replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error:在iOS 6中工作。在iOS 5上调用此方法并正常工作的应用程序在iOS 6上存在主要问题。在运行iOS 6.0以下版本的设备上不会出现此问题。如果应用程序是由Xcode在iOS模拟器中启动的,则不会发生该问题。否则,这个问题似乎是普遍的。replaceItemAtURL:withItemAtURL:backupItemName:options:resultsItemURL:error:broken in iOS 6?

下面是测试代码,我试图执行:

NSError *error; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:@"test.txt"]; 
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test.txt"]; 

// Create initial file in documents directory 
if (![fileManager fileExistsAtPath:destinationPath]) 
{ 
    BOOL fileCopied = [fileManager copyItemAtPath:sourcePath 
              toPath:destinationPath 
              error:&error]; 
    if (!fileCopied) 
     [[self statusLabel] setText:[NSString stringWithFormat:@"Creation Error:\n\n%@", 
            [error localizedDescription]]]; 
} 

// Replace file in documents directory with copy of file from app bundle 
if ([fileManager fileExistsAtPath:destinationPath]) 
{ 
    NSURL *destinationURL = [NSURL fileURLWithPath:destinationPath]; 
    BOOL fileReplaced = [fileManager replaceItemAtURL:destinationURL 
             withItemAtURL:[NSURL fileURLWithPath:sourcePath] 
             backupItemName:nil 
               options:0 
            resultingItemURL:&destinationURL 
               error:&error]; 
    if (!fileReplaced) 
     [[self statusLabel] setText:[NSString stringWithFormat:@"Replacement Error:\n\n%@", 
            [error localizedDescription]]]; 
    else 
     [[self statusLabel] setText:@"Successfully replaced file."]; 
} 

它创建的文件目录中的文件,如果它不存在。然后它尝试用文件夹中的文件副本替换文档目录中的文件。然后它会报告文件创建/替换的状态。正如我之前所说的,如果它在iOS 5或更低版本上运行,或者如果它正在iOS Simulator中运行并且附加了Xcode,它将会取代它。但是,如果它在iOS 6设备或iOS Simulator上运行而没有Xcode,则替换将失败并返回错误。本地化的错误描述是The operation couldn’t be completed. (Cocoa error 512.)

用户信息字典的错误是:

{ 
NSFileNewItemLocationKey = "file://localhost/var/mobile/Applications/487FBB9E-A2BD-4CF2-BB38-F36764623C2F/Test.app/test.txt"; 
NSFileOriginalItemLocationKey = "file://localhost/var/mobile/Applications/487FBB9E-A2BD-4CF2-BB38-F36764623C2F/Documents/test.txt"; 
NSURL = "file://localhost/var/mobile/Applications/487FBB9E-A2BD-4CF2-BB38-F36764623C2F/Documents/test.txt"; 
NSUnderlyingError = "Error Domain=NSCocoaErrorDomain Code=513 \"The operation couldn\U2019t be completed. (Cocoa error 513.)\" UserInfo=0x1d58d350 {NSFilePath=/var/mobile/Applications/487FBB9E-A2BD-4CF2-BB38-F36764623C2F/Test.app/test.txt, NSURLUnsetValueKeysKey=<CFArray 0x1d58d180 [0x39b9d100]>{type = immutable, count = 2, values = (\n\t0 : <CFString 0x39b945b4 [0x39b9d100]>{contents = \"NSURLFileSecurityKey\"}\n\t1 : <CFString 0x39b943d4 [0x39b9d100]>{contents = \"NSURLCreationDateKey\"}\n)}, NSUnderlyingError=0x1d58d010 \"The operation couldn\U2019t be completed. Operation not permitted\", NSURL=file://localhost/var/mobile/Applications/487FBB9E-A2BD-4CF2-BB38-F36764623C2F/Test.app/test.txt}"; 
} 

我在App Store上的应用程序依赖于这种方法。实时应用程序继续在iOS 5上没有任何缺陷地工作,但在iOS 6中,由于方法失败,它具有巨大的问题。有谁知道为什么这种方法失败?

回答

8

NSFileManager方法replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error:不是复制方法;这是一种移动方法。即,替换文件的拷贝并不替换该文件,而是替换文件本身。由于应用程序不应该能够修改自己的包,所以上述代码应该永远不会在任何iOS版本中起作用。

要保留原子性,解决方案是先将替换文件的副本保存到临时目录,然后将该文件替换为临时目录中的副本。

这里是固定的测试代码:

NSError *error; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 

NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"]; 
NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:@"test.txt"]; 

// Create initial file in documents directory 
if (![fileManager fileExistsAtPath:destinationPath]) 
{ 
    BOOL fileCopied = [fileManager copyItemAtPath:sourcePath 
              toPath:destinationPath 
              error:&error]; 
    if (!fileCopied) 
     [[self statusLabel] setText:[NSString stringWithFormat:@"Creation Error:\n\n%@", [error localizedDescription]]]; 
} 

// Replace file in documents directory with copy of file from app bundle 
if ([fileManager fileExistsAtPath:destinationPath]) 
{ 
    // Create temporary file 
    NSString *tempPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"test.txt"]; 

    BOOL tempCopied = [fileManager copyItemAtPath:sourcePath 
              toPath:tempPath 
              error:&error]; 
    if (!tempCopied) 
     [[self statusLabel] setText:[NSString stringWithFormat:@"Temp Creation Error:\n\n%@", [error localizedDescription]]]; 

    // Replace file with temporary file 
    NSURL *destinationURL = [NSURL fileURLWithPath:destinationPath]; 

    BOOL fileReplaced = [fileManager replaceItemAtURL:destinationURL 
             withItemAtURL:[NSURL fileURLWithPath:tempPath] 
             backupItemName:nil 
               options:0 
            resultingItemURL:&destinationURL 
               error:&error]; 
    if (!fileReplaced) 
     [[self statusLabel] setText:[NSString stringWithFormat:@"Replacement Error:\n\n%@", [error localizedDescription]]]; 
    else 
     [[self statusLabel] setText:@"Successfully replaced file."]; 
}