将OSX文件夹复制到iOS目录

时间:2012-10-30 11:25:26

标签: ios objective-c xcode macos filesystems

我正在尝试将文件夹及其内容复制到文档目录中的子目录,但是它失败并显示错误: “操作无法完成。没有这样的文件或目录”

首先,我尝试在文档目录中创建一个文件夹,如下所示:

NSString *diagramsDirectory = [docDirectory stringByAppendingPathComponent:@"Diagrams"];
if (![fileManager fileExistsAtPath:docDirectory isDirectory:&isDirectory] || !isDirectory)
{
    NSError *error = nil;
    NSDictionary *attr = [NSDictionary dictionaryWithObject:NSFileProtectionComplete
                                                     forKey:NSFileProtectionKey];
    [fileManager createDirectoryAtPath:diagramsDirectory
           withIntermediateDirectories:NO
                            attributes:attr
                                 error:&error];
    if (error) {
        NSLog(@"error creating dir. path: %@", [error localizedDescription]);
    }
}
NSLog(@"diagrams directory = %@", diagramsDirectory);

控制台日志似乎表明这有效:

diagrams directory = /Users/../iPhone Simulator/../Library/Documentation/Diagrams

但是,当我尝试从Mac上的目录中复制名为“Diagrams”的文件夹时:

NSString *pathToDirectories = @"/User/Desktop/Project Resource Files/Files/";
NSError *error = nil;
NSArray *folders = [fileManager contentsOfDirectoryAtPath:pathToDirectories error:&error];   
for (NSString *folder in folders) {      
    if ([folder isEqualToString:@"Diagrams"]) {
        [self copyFolderAtPath:folder toDestinationFolderAtPath:docDirectory];            
    }

调用“copyFolderAtPath”方法:

- (BOOL)copyFolderAtPath:(NSString *)sourceFolder toDestinationFolderAtPath:(NSString *)destinationFolder
{
    destinationFolder = [destinationFolder stringByAppendingPathComponent:[sourceFolder lastPathComponent]];    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = nil;

    // check for destination folder
    if ([fileManager fileExistsAtPath:destinationFolder])
    {
        if (![fileManager removeItemAtPath:destinationFolder error:&error])
        {
            NSLog(@"Could not remove old files. Error: %@", error);
            return NO;
        }
    }
    error = nil;
    // copy destination
    if (!([fileManager copyItemAtPath:sourceFolder toPath:destinationFolder error:&error])) {
        NSLog(@"failed copying file at path %@ to path %@. Error %@", sourceFolder, destinationFolder, error);
        return NO;
    }
    return YES;
}

它返回“no”,我收到错误。

任何人都知道我做错了什么?

1 个答案:

答案 0 :(得分:1)

设备(以及模拟器)与操作系统隔离,因此您无法直接执行文件系统副本。想象一下,即使它让你从模拟器中做到这一点,运行应用程序的断开连接的设备如何访问操作系统文件系统?

您必须查看其他选项,例如在Mac上设置opens sockets或在设备复制的mac上具有http端点的应用程序。其他选项包括同步文档via iCloudanother cloud service。你也可以transfer files via iTunes。我确信还有很多其他选择......还要结帐this