2

For the long time I receive a strange error, but only when running on a device. In simulator everything is just fine. What I do is moving sample files from app bundle directory to Documents directory on a device. I have tried all those methods for NSFileManager:

- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error
- (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error
- (BOOL)linkItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error

And every one throws errors like following:

2015-08-15 23:14:15.152 app[1010:339938] Error: Error Domain=NSCocoaErrorDomain Code=513 "“file.txt” couldn’t be linked because you don’t have permission to access “Documents”." UserInfo={NSSourceFilePathErrorKey=/var/mobile/Containers/Bundle/Application/verylongid/fastdict.app/Dictionaries/file.txt, NSUserStringVariant=( Link ), NSDestinationFilePath=/var/mobile/Containers/Data/Application/verylongid/Documents/file.txt, NSFilePath=/var/mobile/Containers/Bundle/Application/verylongid/fastdict.app/Dictionaries/file.txt, NSUnderlyingError=0x13666d5a0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}} in -[AppDelegate fillDirectoryWithSamplesIfEmpty]

How can I solve this problem?

Update: The solution found here really works! Good luck!

Community
  • 1
  • 1
ashvardanian
  • 424
  • 1
  • 6
  • 17
  • Possible duplicate of [iPhone - copying a file from resources to Documents gives an error](http://stackoverflow.com/questions/1132005/iphone-copying-a-file-from-resources-to-documents-gives-an-error) – Ajith Renjala May 18 '16 at 08:40

1 Answers1

6

You can't able to move the bundle file to document directory, since you dont have a permission to do. Try copyItemAtPath Also Make sure that you are getting the document directory correctly

  NSString *fileName = @"LICENSE.txt";
    NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
    NSData *mainBundleFile = [NSData dataWithContentsOfFile:sourcePath];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectoryPath = [paths objectAtIndex:0];

    [[NSFileManager defaultManager] createFileAtPath:[NSString stringWithFormat:@"%@/%@", documentsDirectoryPath,fileName]
                                            contents:mainBundleFile
                                          attributes:nil];
    NSString* content = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", documentsDirectoryPath,fileName] encoding:NSUTF8StringEncoding
                                                     error:NULL];
    self.label.text = content;
    NSLog(@"file content = %@",content);

The above code works perfectly in iPhone5 device iOS9.0 . Still problem exists , paste your code snippet

karthikPrabhu Alagu
  • 3,371
  • 1
  • 21
  • 25
  • Its one single line of code. Totally same to what you wrote. You can see the error description in my question post. – ashvardanian Aug 17 '15 at 09:53
  • Are you tested in iOS9 beta installed device ? – karthikPrabhu Alagu Aug 17 '15 at 09:57
  • 1
    Oh god... You answered me three times and every time it seems like you are not reading the question. Take a look at it again! Its an issue with iOS9 (it even has such a tag!). And in post i wrote, that it only occurs on device only, not in simulator... Please, be more watchful. – ashvardanian Aug 17 '15 at 10:01
  • 1
    @AshotVardanian Ok man.. cool..Just want to reconfirm..cool...Still you didn't post your code snippet... Actually i don't have a iOS9 installed device . Is it working on iOS8.x installed devices ? – karthikPrabhu Alagu Aug 17 '15 at 10:06
  • 1
    @AshotVardanian updated answer. The code is working perfectly on my iPhone5 iOS9.0 device. – karthikPrabhu Alagu Aug 17 '15 at 13:16