Differentiate Internal vs External URL

Given a NSURL, I would like to know if it is internal to the machine (including connected disks) or external. For movies to be played by AVPlayer, in the former case, we copy the file into our CoreData file store to make the document self-contained. In the latter, we would like to just store the URL, or possibly give the user the option to copy it to internal. What is the most reliable way to tell the difference?

Replies

This is possible and fairly straight forward, just use NSURLComponents class. It supports that very feature. You can compare hosts or once you have the query string, compare and contrast to your server host address.

Thanks, but that's not really the issue. The URL might have come from anywhere, e.g., typed in from a scrap of paper. I just need to evaluate a bool which saye if it is external to the machine so I can decide whether to store the file, or just store the URL. Is [NSURL host] always the same for a local file? Is that something I can easily evaluate?


This is an educational application. What I want is to know what is (reliably) common to all local URLs. User may want to just reference a movie clip enbedded in some web page. If user can look at the page source and extract the movie URL,user can drag that URL into the document without copying the file. Then, assuming user is connected to the internet, the URL can be played later.

This is not easy. The problem breaks down as follows:

  • file URLs

  • well-known network URLs (http, https, ftp, and so on)

  • other URLs

For other URLs there's obviously no general-purpose solution; you'll have to handle these on a case-by-case basis or simply treat them all as external.

For network URLs, the first problem is one of definition. If the network URL points to a server that's running on the local machine, is that internal or external? If you consider it external, you're done: all network URLs are external. If you consider it internal, you have more work to do. You could resolve the host to see if the resulting IP address matches one of the machine's local IP addresses, but that starts getting really ugly really fast. A host can resolve to multiple IP address (and don't forget that some might be v4 and some might be v6), so what do you do if you get conflicting answers?

For file URLs the problem is, again, one of definition. The file URL might point to a file server, in which case you want to treat it like a network URL. But even locally attached volumes present problems. For example, is a USB thumb drive considered internal? I suspect it's not, because it's reasonable to expect that the user might unmount the drive. What about a second partition on the internal hard drive? What do you want to do with network home directories (a very real possibility in education deployments)? Argh!

My first cut at this would probably be:

  1. if it's not a file URL (

    -isFileURL
    ), treat it as external
  2. if it is a file URL, if it's not on the same volume as the referencing document, treat it as external

  3. otherwise (it's a file URL on the same volume as the referencing document), treat it as internal

You can tell if two file URLs are on the same volume by checking the

NSURLVolumeIdentifierKey
value.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

You're right. I guess it's not that simple. What about using [NSFileManager isReadableFileAtPath:]? This would take in attached drives, including thumb drives, maybe iCloudDrive, etc., in which case we would just copy the file into the internal database. If not, we would just store the URL. I'm not really sure what NSFileManager does with a URL to a file on some remote website or other server.


I'm trying to take advantage of AVPlayer's ability to play anything that is accessible and compatible. I already have this in an impromptu view where the user can drag in a web-based URL for a video and play it. Most videos are internalized in the database, but those require managing "File" objects for access. I want the ability to store, and later play, a URL (that can't be internalized) from outside the database, e.g., a web site. That's why I need to differentiate.

What about using [NSFileManager isReadableFileAtPath:]?

It is essentially a wrapper around

access
.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Actually, this was getting too complicated so I took another approach. I realized that a dragged in URL was going to be interpreted as text, and I would need to employ a data detector to tell the difference. ***** it. I simply added a popover to (optionally)allow setting a movie text URL in my inspector panel, which I store as a NSURL. Then, in a few places, I had to allow the AVPlayer to get its URL from the stored version, if present. Problem solved.