Steven Lu
Steven Lu

Reputation: 43417

NSURL for a downloaded or otherwise obtained file to open an iOS app with: What is its filepath?

I have found that so far the path sent to application:openURL:sourceApplication:annotation: is:

file://localhost/private/var/mobile/Applications/<GUID>/Documents/Inbox/file

To check that the filesystem operations that I am about to perform are indeed likely to succeed (and that the url given to me is not a location outside the sandbox), it looks like I have to do this:

NSString* hdurl = [[@"file://localhost/private" stringByAppendingString:NSHomeDirectory()] stringByAppendingString: @"/"];
NSString* path = url.absoluteString;
if ([path hasPrefix:hdurl]) {
    // now ready to e.g. call fopen on: [path substringFromIndex:@"file://localhost".length]

Now, I seem to vaguely recall (and this is probably wrong) that in the past I have seen the file:/// style URL being used. That would clearly cause this code to fail.

How am I to know that it will always give me a file://localhost URL prefix?

Apple's documentation on URLs is strangely missing a section on file URLs.

Upvotes: 1

Views: 122

Answers (1)

rmaddy
rmaddy

Reputation: 318774

An NSURL that points to a file on the local file system is called a "file URL". To convert the NSURL to an NSString representing the file's path you use:

NSString *filePath = [url path];

To check to see if an NSURL represents a file URL, use:

BOOL isFileURL = [url isFileURL];

Keep in mind that if your app is passed a file URL, you will always have access to the file. There is no need to check if it starts with any prefix. Why would iOS pass you a file that you don't have access to?

Upvotes: 1

Related Questions