gpt4 book ai didi

iphone - replaceItemAtURL :withItemAtURL:backupItemName:options:resultingItemURL:error: broken in iOS 6?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:03:57 30 4
gpt4 key购买 nike

我无法让 NSFileManager 方法 replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error: 在 iOS 6 中工作。调用此方法并在 iOS 5 上运行良好的应用程序在 iOS 上存在重大问题6. iOS 6.0以下版本的设备不会出现该问题。如果应用程序由 Xcode 在 iOS 模拟器中启动,则不会出现此问题。否则这个问题似乎是普遍的。

这是我要执行的测试代码:

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 设备或没有 Xcode 的 iOS 模拟器上运行,替换将失败并返回错误。本地化的错误描述是 The operation couldn't be completed. ( cocoa 错误 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 上由于方法失败而出现巨大问题。有谁知道为什么这个方法失败了?

最佳答案

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."];
}

关于iphone - replaceItemAtURL :withItemAtURL:backupItemName:options:resultingItemURL:error: broken in iOS 6?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13565867/

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