Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if file exists at URL instead of path

How can I check if a file exists at a URL (instead of a path), in order to set a pre-populated default store into the iPhone Simulator:

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Food.sqlite"]; /*  Set up the store.  For the sake of illustration, provide a pre-populated default store.  */ NSFileManager *fileManager = [NSFileManager defaultManager]; // If the expected store doesn't exist, copy the default store. if (![fileManager fileExistsAtPath:storePath]) {     NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Food" ofType:@"sqlite"];     if (defaultStorePath) {         [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];     } } 

I've read that in recent versions of the templates, the applicationDocumentsDirectory method returns an URL, so I've changed the code to use NSURL objects to represent the file path. But at [fileManager fileExistsAtPath:storePath], I need to change fileExistsAtPath to something like fileExistsAtURL (obviously it doesn't exist).

I've checked the NSFileManager Class Reference and I didn't find any suitable task suited for my purpose.

Any hints please?

like image 724
Pedro Sousa Avatar asked Oct 24 '11 16:10

Pedro Sousa


People also ask

How do you check if a file exists from a URL?

get_headers() will return an array with the headers from an HTTP request. The first index has the HTTP response code, HTTP code 200 means the request was a success (file exists). You will get a 404 code if nothing exists for the request. If the “200 OK” is found then true is returned else false is returned.

How do I check to see if a file exists?

To check if a file exists, you pass the file path to the exists() function from the os. path standard library. If the file exists, the exists() function returns True . Otherwise, it returns False .

How do I find the URL of a file?

To obtain a file or folder's URL, to the right of the file or folder, from the Actions drop-down menu, select Edit Details. This displays the Edit Details page for the item.


1 Answers

if (![fileManager fileExistsAtPath:[storeURL path]])  ... 

From the documentation:

If this URL object contains a file URL (as determined with isFileURL), the return value of this method is suitable for input into methods of NSFileManager or NSPathUtilities. If the path has a trailing slash it is stripped.

like image 65
Mundi Avatar answered Oct 04 '22 09:10

Mundi