Testing file existence using NSURL

NSURL does have this method:

- (BOOL)checkResourceIsReachableAndReturnError:(NSError **)error

Which “Returns whether the resource pointed to by a file URL can be reached.”

NSURL *theURL = [NSURL fileURLWithPath:@"/Users/elisevanlooij/nonexistingfile.php" 
               isDirectory:NO];
NSError *err;
if ([theURL checkResourceIsReachableAndReturnError:&err] == NO)
    [[NSAlert alertWithError:err] runModal];

Leave a Comment