Getting NSURL of System Root Directory - objective-c

I want to get the NSURL of the root directory. I have tried this:
NSURL *rootURL = [NSURL fileURLWithPath:#"/" isDirectory:YES];
but it prints
file://localhost/
I want my output to be
file:///
Any help on this simple one?

This should work:
NSURL *url = [NSURL URLWithString:#"file:///"];
It returns what you want.

file://localhost/ is valid.. I don't know if file:/// is also a valid url but if it would be, they are identical.

Related

NSURL fileUrlWithPath method returns double path

this is the scenario:
In a sandboxed app for OS X 10.10.5 I have some path saved in NSString object, say #"file:///Users/xxx/".
Then I execute [NSURL fileURLWithPath:object]. That gives me NSURL object like this
#"file:/Users/xxx --
file:///Users/xxx/Library/Containers/com.123456.App/Data/"
.
I only need this part #"file:///Users/xxx/Library/Containers/com.123456.App/Data/"
Somehow the source string is twisted and doubled and extra dashes added in the middle.
Can anyone explain why does it happen?
Xcode 6.4
fileURLWithPath: will return a file URL path.
i.e starting with: file:///
This means that the string path that you pass it should be in the form of:
#"/Users/xxx/Library/Containers/com.123456.App/Data/"
You do not need to prepend the path with file:///. Or you will get the result that you are getting.
Example:
NSString * stringPath = #"/Users/xxx/Library/Containers/com.123456.App/Data/";
NSURL * anUrl =[NSURL fileURLWithPath:stringPath ];
NSLog(#"nUrl %#",anUrl);
----> nUrl file:///Users/xxx/Library/Containers/com.123456.App/Data/
Can you try something like this? By giving fileName and extension.
NSURL *url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"fileExtension"]];

How to see the path of a NSURL bookmark even if it's unavailable

I'm saving a NSURL given from a save panel in the user preferences. I'm wondering how to see the path of the URL even if the device on which the file resides is not available, i.e. [NSURL URLByResolvingBookmarkData:options:bookmarkDataIsStale:error:] returns nil.
The last known path of a bookmark can be retrieved like so:
NSDictionary *values = [[NSURL resourceValuesForKeys:#[NSURLPathKey]
fromBookmarkData:bookmarkData]
NSString *path = [values objectForKey:NSURLPathKey];
I made a full writeup of this.

Checking if file exists when file on an external device

A playlist file .m3u contains entries available on an external device (a USB key in this case) such as:
/Volumes/KINGSTON/folder/mytitle.mp3
I'd like to check if the file exists:
NSURL *url = [NSURL URLWithString:#"/Volumes/KINGSTON/folder/mytitle.mp3"];
NSFileManager *manager = [NSFileManager defaultManager];
NSLog(#"%d",[manager fileExistsAtPath:[url absoluteString]]); //returns 0. I expect 1
I also tried:
NSURL *u = [[NSURL alloc]initWithScheme:#"/Volumes" host:#"/KINGSTON" path:#"/folder/mytitle.mp3"];
NSLog(#"%d",[manager fileExistsAtPath:[u absoluteString]]); //0
What did I do wrong?
Thanks,
Roland
NSURL *url = [NSURL URLWithString:#"/Volumes/KINGSTON/folder/mytitle.mp3"];
That string does not describe a URL. It's a pathname. Use fileURLWithPath:.
NSLog(#"%d",[manager fileExistsAtPath:[url absoluteString]]);
absoluteString does not return a path; it returns a string describing a URL. Use path.
Or, better yet, use checkResourceIsReachableAndReturnError:.
I also tried:
NSURL *u = [[NSURL alloc]initWithScheme:#"/Volumes" host:#"/KINGSTON" path:#"/folder/mytitle.mp3"];
/Volumes isn't a scheme, /KINGSTON isn't a host, and /folder/mytitle.mp3 is a path but does not refer to anything that exists.
The scheme for a file URL is file:, and the host is generally either localhost or the empty string. The path of a file URL is the complete absolute path to the file.
In your first example, you need to use +[NSURL fileURLWithPath:]. In your second example, I see what you're trying to do, but you're simply going about it the wrong way.
I assume there's a reason you're bothering with NSURL when you have the path you could pass directly to -fileExistsAtPath:?

Can't instantiate NSURL with file path

When creating an NSURL with [NSURL fileURLWithPath:[#"~/Movies" stringByExpandingTildeInPath]], I get following error in the console:
-[NSURL initWithScheme:host:path:]: path file:/localhost/Users/michael is not absolute.
It worked while compiling in Debug mode, this problem occured only after switching to Release.
EDIT: Just to clarify, I get the error message at run time when the NSURL object is initialized, not while building.
I think your code needs to look like:
NSURL *url = [NSURL fileURLWithPath:[#"~/Movies" stringByExpandingTildeInPath]];
Looks to me like you are missing the #"" that needs to surround '~/Movies'.
works fine here ...
NSURL *url = [NSURL fileURLWithPath:[#"~/Movies" stringByExpandingTildeInPath]];
NSLog(#"url: %#", url);
BUT
is your app sandboxed in release mode? That could explain this

NSURL fileURLWithPath where NSString has a space

I've looked at quite a few of the related questions and cannot find a similar problem or a solution so my apologies if there is a duplicate out there somewhere.
Anyway, I'm trying to generate a file's NSURL to use with an NSXMLDocument. I have the following components:
const NSString * PROJECT_DIR = #"~/SP\\ BB/";
const NSString * STRINGS_FILE = #"Localizable.strings";
and construct the URL like so:
NSURL * stringsURL = [NSURL fileURLWithPath:[[NSString stringWithFormat:#"%#%#",PROJECT_DIR,STRINGS_FILE] stringByExpandingTildeInPath]];
however, the resulting path in the NSURL is:
file://localhost/Users/timothyborrowdale/SP2B/Localizable.strings
I have tried changing the PROJECT_DIR to
#"~/SP BB/"
#"~/SP\\\\ BB/" (changes to SP엀2B)
#"~/SP%20BB/"
#"~/SP\%20BB/"
with the same problem. I also tried typing out the file url completely and using [NSURL URLWithString:]
I have also tried using stringByAddingPercentEscapesUsingEncoding with both NSUTF8Encoding and NSASCCIEncoding and these have the same issue.
The NSString displays properly before being passed to NSURL or stringByAddingPercentEscapesUsingEncoding but has the problem once outputted from either.
Try this:
NSString *fnam = [#"Localizable" stringByAppendingPathExtension:#"strings"];
NSArray *parts = [NSArray arrayWithPathComponents:#"~", #"SP BB", fnam, (void *)nil];
NSString *path = [[NSString pathWithComponents:parts] stringByStandardizingPath];
NSURL *furl = [NSURL fileURLWithPath:path];
Foundation has a host of platform-independent, path-related methods. Prefer those over hard-coding path extension separators (often ".") and path component separators (often "/" or "\").
Try abandoning stringWithFormat: (never the right answer for stapling paths together) and stringByExpandingTildeInPath and using NSHomeDirectory() and stringByAppendingPathComponent: instead.
#"~/SP\\ BB/" (changes to SP엀2B)
How did you arrive at that conclusion?