3

fileExistsAtPath and checkResourceIsReachableAndReturnError both returns NO Even the file exists at server URL

I have a file in (local)server in http://10.0.0.15/images/hibrise.png

  NSURL *url=[NSURL URLWithString:@"http://10.0.0.15/images/hibrise.png"];

BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[url path]];
 NSLog(fileExists ? @"Yes" : @"No");

I have checked the file permissions also its fine. When I type the same url in my browser it presents the image

'fileExistsAtPath' This method is used to check the file availability in internal file system

checkResourceIsReachableAndReturnError This is also only for the internal iOS File System This method is currently applicable only to URLs for file system resources. For other URL types, NO is returned Apple says the same for fileExistsAtPath

What is the best way to check file availability using URL

AlgoCoder
  • 1,706
  • 5
  • 19
  • 40
  • it's Better to save the url in to NSCache and check that it found in to NScache or not. becouse You cannot check file url with NSFileManager – Nitin Gohel Mar 22 '14 at 07:58

2 Answers2

0

NSFileManager is for internal iOS file system. You can not check if remote file is exist or not using NSFileMAnager.

Use the method checkResourceIsReachableAndReturnError of class NSURL instead.

Apurv
  • 17,116
  • 8
  • 51
  • 67
  • 3
    Thanks for the reply but This is also only for the internal iOS File System `This method is currently applicable only to URLs for file system resources. For other URL types, NO is returned` – AlgoCoder Mar 22 '14 at 07:52
0

You cannot check a file in a location other than the device using the NSFileManager class.

What you need to use is NSURL method - (BOOL)checkResourceIsReachableAndReturnError:(NSError **)error

NSURL *url=[NSURL URLWithString:@"http://10.0.0.15/images/hibrise.png"];
NSError *err;
BOOL fileExists = [url checkResourceIsReachableAndReturnError:&err];

If there was an error, it will be passed to the NSError pointer so you should also check this

Correct answer is to actually perform an HTTP HEAD request and check for a 404 error:

BOOL fileExists;
NSURL *url=[NSURL URLWithString:@"http://10.0.0.15/images/hibrise.png"];
    NSMutableURLRequest *headRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0f];
    [headRequest setHTTPMethod:@"HEAD"];
    [NSURLConnection sendAsynchronousRequest:headRequest
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                               fileExists = ([(NSHTTPURLResponse *)response statusCode]!=404)
                           }];
Lefteris
  • 14,550
  • 2
  • 56
  • 95
  • This is cann't help for this question. From apple's doc : `This method is currently applicable only to URLs for file system resources. For other URL types, NO is returned ` – Mani Mar 22 '14 at 08:33
  • Thanks! The updated answer was what I needed. In my case I was getting a 403 status code (not 404) on fails for AWS S3 files. I changed the check to statusCode == 200. – Devin Pitcher Jan 25 '17 at 05:06