0

我想将位于/Library文件夹中的文件复制到/User/Library/AddressBook/Sample/,我使用了:

[[NSFileManager defaultManager] copyItemAtPath: @"/Library/MyFile.mp3" 
                                        toPath: @"/User/Library/AddressBook/Sample/MyFile.mp3" 
                                         error: &error];

但是我遇到了一个错误,提示“无法完成操作。没有这样的文件或

目录`

我正在使用越狱的 iPhone。

4

2 回答 2

0

目录:

/User/Library/AddressBook/Sample/

手机上一般不存在。Sample在尝试将 mp3 文件复制到其中之前,您是否添加了子目录?

使用这些NSFileManager方法,我还建议使用错误对象来帮助您调试:

NSError* error;
[[NSFileManager defaultManager] copyItemAtPath:@"/Library//MyFile.mp3" toPath: @"/User/Library/AddressBook/Sample/MyFile.mp3" error:&error];

if (error != nil) {
    NSLog(@"Error message is %@", [error localizedDescription]);
}

此外,您的拼写似乎有错误copyItemAtPath,但可能只是在您的问题中,而不是在您的代码中?无论如何,请仔细检查。

而且,您的路径中也有一个双斜杠 ( //),但我认为这不会伤害您。把它拿出来,打字时要小心:)

更新

如果您只是正常运行此应用程序但在越狱手机上,您的应用程序将无法访问这些目录。在越狱手机上正常安装的应用程序仍然是沙盒。越狱不会删除手机上的所有规则。如果您将应用程序安装在 中/Applications,就像真正的越狱应用程序一样,那么该代码应该适合您。

于 2013-03-22T08:15:57.230 回答
0
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
NSLog(@"%@",libraryDirectory); // Library path
NSString *AddressBookPath = [libraryDirectory stringByAppendingPathComponent:@"AddressBook"];

if (![[NSFileManager defaultManager] fileExistsAtPath:AddressBookPath])
{        
    NSError* error;
    // Create "AddressBook Dir"
    if([[NSFileManager defaultManager] createDirectoryAtPath:AddressBookPath withIntermediateDirectories:NO attributes:nil error:&error])
    {
        // Create "Sample Dir"
        NSString *samplePath = [AddressBookPath stringByAppendingPathComponent:@"Sample"];
        if (![[NSFileManager defaultManager] fileExistsAtPath:AddressBookPath])
        {                
            NSError* error;
            if([[NSFileManager defaultManager] createDirectoryAtPath:AddressBookPath withIntermediateDirectories:NO attributes:nil error:&error])
            {
                // Copy Files Now
                NSError* error;
                NSString *fromPath = [libraryDirectory stringByAppendingPathComponent:@"MyFile.mp3"];
                NSString *toPath = [samplePath stringByAppendingPathComponent:@"MyFile.mp3"];
                [[NSFileManager defaultManager] copyItemAtPath:fromPath toPath:toPath error:&error];

                if (error != nil)
                {
                    NSLog(@"Error message is %@", [error localizedDescription]);
                }
            }
        }
    }
    else
    {
        NSLog(@"[%@] ERROR: attempting to write create MyFolder directory", [self class]);
        NSAssert( FALSE, @"Failed to create directory maybe out of disk space?");
    }
}
于 2013-03-22T08:50:46.057 回答