Search code examples
iosios7nsdatansfilemanager

dataWithContentOfFile return nil


I'm using dataWithContentsOfFile to read data but it returns nil when i close the app and come back. Strange thing is when i try to access in the same run when i captured the image and saved it in Documents or tmp folder, i do get the image data back. But, when i close the app, come back and read the data, it returns nil.

I've tried using dataWithContentsOfFile:options:error: but it give me error like this:

Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x14d6fb50 {NSFilePath=/var/mobile/Applications/495A5193-B8C2-4FE8-A745-7BA20221022A/Documents/ProjectName_Area_83554355-FC14-4B01-B53C-C6143633B8F5, NSUnderlyingError=0x14d6add0 "The operation couldn’t be completed. No such file or directory"}

Also, if i access the appdata folder, image exists there in the above mentioned path.

I'm guessing it might be because of iOS8 sdk. But, in current scenario i'm developing on iOS8.1 sdk and running it on iOS7.1.

Any cross question to help are appreciated. Cheers.

Edit:

When i go to the path mentioned in error and try to upload file from POSTMAN (directly from the path), it give me an "invalid file type" error. So, i guess file is there but some how, my code can't read it.


Solution

  • The problem was with the documents path, i.e. on each run iOS 7 and later makes a new path each time. Here is an example:

    The path you try to access will be some thing like this:

    var/../SE33JI1-9D8GD9F-9DG34J5HJ-LK345DF8DS/YOUR_APP/DOCUMENTS/../an_example_name.png
    

    So this important thing here 'SE33JI1-9D8GD9F-9DG34J5HJ-LK345DF8DS' is an identifier before your app name, in image path. Each time you run your app, iOS assigns a new value in place of this identifier due to security of the app content. And new additions to iCloud may be.

    So the solution is to get the documents path each time app runs, instead of saving full path of the image's directory.