NSURL fileUrlWithPath method returns double path - objective-c

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"]];

Related

Getting NSURL of System Root Directory

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.

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:?

How can I append a hashtag to local html file URL obtained with fileURLWithPath?

I am trying to build a URL string that will be passed to UIWebView object. Up until now I have been pointing to remote files and have now moved the files into the main bundle.
I imported my files using the "create folder reference" option and want to point to the index.html file inside of a folder named 'html'.
I need to append hashtags to the URL generated by the code below but keep getting this error:
-[NSURL stringByAppendingString:]: unrecognized selector sent to instance
I can't figure out why when casting my variables as a NSString I am receiving this error when trying to append to it.
Here is my code:
NSInteger rowNumber = indexPath.row;
NSInteger metroRank = rowNumber+1; //sorted by rank, adding one because of zero-based array
NSString *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"html"]];
NSString *urlSuffix = [NSString stringWithFormat:#"#pg=metro&view=%d", metroRank];
NSString *metroViewURL = [url stringByAppendingString:urlSuffix];
When the above code hits 'stringByAppendString' that is when I receive the error.
Convert it to a NSString first:
NSString *newURL = [[oldURL absoluteString] stringByAppendingString:#"#myhashtag"];
[NSURL fileURLWithPath:] does not return a NSString, but a NSURL. NSURL does not implement the stringByAppendingString selector. You can use URLByAppendingPathComponent: instead.

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?