简体   繁体   中英

copy OSX folder to iOS directory

I'm trying to copy a folder and it's contents to a sub-directory in the documentation directory and it's failing with the error: "The operation couldn't be completed. No such file or directory"

First I try to create a folder in the documentation directory like this:

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

The console log seems to indicate this works:

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

However, when I then try to copy a folder called "Diagrams" from a directory on the Mac:

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

which calls the "copyFolderAtPath" method:

- (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;
}

it returns "no" and I get the error.

Anyone got an idea what I'm doing wrong?

The device (and therefore the simulator) is isolated from the operating system so you cannot directly do file system copies. Imagine even if it let you do it from the simulator, how would a disconnected device running your app access the OS filesystem?

You will have to look into other options like having an application on the mac that opens sockets or having an http end point on the mac that the device copies from. Other options include syncing documents via iCloud or another cloud service . You can also transfer files via iTunes . I'm sure there's many other options ... Also checkout this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM