简体   繁体   中英

NSFileManager: Copy file not success

I want to copy files located at /Library to the folder /User/Library/AddressBook/Sample/ , I used:

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

But I encountered an error that says `Operation could not be completed. No such file or

directory`

I am working on a jailbroken iPhone.

The directory:

/User/Library/AddressBook/Sample/

does not exist on a phone normally. Have you added the Sample subdirectory, before trying to copy the mp3 file into it?

With the NSFileManager methods, I would also recommend using the error object to help you debug:

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

Also, it looks like there is a mistake in your spelling of copyItemAtPath , but probably it's only in your question , and not in your code ? Anyway, please double-check.

And, you have a double-slash ( // ) in your path, too, but I don't think that's hurting you. Just take it out, and be careful when typing :)

Update

If you are just running this app normally , but on a jailbroken phone, your app won't have access to those directories. Apps installed normally, on a jailbroken phone, still are sandboxed . The jailbreak doesn't remove all the rules on the phone. If you install the app in /Applications , like true jailbreak apps are, then that code should work for you.

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?");
    }
}

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