How to convert a file path into an NSURL (and back)

To convert a file path into an NSURL:

To create a local path from an NSURL:

NSURLs can also be created directly from an NSString:

If you’re ever tried to pass a local path into this method you’ll have noticed that it doesn’t work. Use the above methods instead.

You can also add path components to a URL, for example to reference your Documents directory:

will give something like

NSURLs have the same method called URLByAppendingPathComponent:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.