Search code examples
objective-ciphoneipadios7ios8

Library/Caches path not found in iOS 8


I am trying to save images into my local resource path. I have write the below code to to save images in local.

NSString *resoursePath=[[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Library/Caches"]];
NSString *filePath=[NSString stringWithFormat:@"%@/%@", resoursePath, fileName];
BOOL isDirectory=NO;
if(![[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory])
{
     NSError *error;
     if([self writeToFile:filePath options:NSDataWritingAtomic error:&error])
        NSLog(@"Successfully saved");
     else
        NSLog(@"%@", error);
}

It's works fine with iOS 7. But the problem arise in iOS 8. It's write the following error in console.

Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)" UserInfo=0x7f7f8adb0cf0 {NSFilePath=/Users/tapas/Library/Developer/CoreSimulator/Devices/1F61698C-FAF4-4F27-9DA1-14B3F7227308/data/Containers/Bundle/Application/A38802B2-CC82-4220-BA2D-A0FD225016D7/Library/Caches/635471351432677268_iPhoneThumb.jpg, NSUserStringVariant=Folder, NSUnderlyingError=0x7f7f8ae0c100 "The operation couldn’t be completed. No such file or directory"

Please suggest me what to do. Thanks in advance.


Solution

  • At last I found the answer. This work in iOS 6,7 as well as 8. Just need to change the below line.

    From

    NSString *resoursePath=[[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Library/Caches"]];
    

    To

    NSString *resoursePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];