Search code examples
iosiphoneios8nsfilemanagerfile-exists

fileExistsAtPath: Does Not Return Correct Value in iOS 8 SDK


My app was working perfectly in iOS 7, and now I'm facing some new bugs after switching to iOS 8 SDK.

For Ex. [NSFileManager defaultManager] file existence checking method ( fileExistsAtPath: ) is no longer working. Here's the details about current situation in my code:

  1. This is the block of code whose condition never turns True :

    File *tempFile = currentMessage.contains;
    NSString *address = tempFile.thumbAddress;
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:currentMessage.contains.thumbAddress])
    {
        image = [UIImage imageWithContentsOfFile:currentMessage.contains.thumbAddress];
    }
    
  2. I have put a breakpoint before the if and traced it to see what is contains :

    Printing description of address:
    /Users/Unkn0wn/Library/Developer/CoreSimulator/Devices/C07E1D76-372E-4C9A-8749-50D369294FBA/data/Containers/Data/Application/1DC7DB3A-B0A2-43AE-9EDA-3E121786D1AA/Documents/FileAt141406917489510096thumbnail.jpg
    
  3. I have checked the Finder to see if the file actually exists ( the address is copied from console so there's no chance of mistyping and typos in checking the path and file existence ) :

enter image description here

But that if block is not executed because fileExistsAtPath: returns NO.

Am I missing anything or it is just a bug of new SDK ? ( Because it was working perfectly with SDK 7.1.1 )

Any suggestions is highly appreciated.

Thanks


Solution

  • If thumbAddress is a full path (not a relative one), it can cause bugs when the app is updated because the app folder changes, for example, your first version of the app could be at that path:

    /Users/Unkn0wn/Library/Developer/.../Application/ABCD/
    

    When you update the app, the directory will change, for example:

    /Users/Unkn0wn/Library/Developer/.../Application/EFGH/
    

    So if you save a full path for your image in the first version of the app like :

    ABCD/yourImage.jpg
    

    And try to retrieve this image with the new version of the app, this image will not be found anymore as your image will now be at the path:

    EFGH/yourImage.jpg
    

    Hopes it helps!


    EDIT

    Maybe your absolute path is incorrect. Try calling it with a relative path:

    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    path = [path stringByAppendingString:@"FileAt141406917489510096thumbnail.jpg"];
    
    NSData *imageData = [NSData dataWithContentsOfFile:path]; // Should be != nil