Can't instantiate NSURL with file path - objective-c

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

Related

Reading jpeg from file fails with "no such file" but file is clearly there

I'm running into problems reading jpeg files from the file system and displaying the image in an NSImage. Here's a snippet of code:
NSError *myError;
NSString *path = #"file:/Users/jpurlia/Documents/Development/Test/1915brsts880804of.jpg";
NSURL *url = [NSURL fileURLWithPath:path];
NSData *data = [NSData dataWithContentsOfURL:url
options:0
error:&myError];
_photoImageView.image = [[NSImage alloc] initWithData:data];
Running this code generates the following error upon calling dataWithContentsOfURL:
Error Domain=NSCocoaErrorDomain Code=260 "The file “1915brsts880804of.jpg” couldn’t be opened because there is no such file."
And, additionally:
{Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}
The file does exist, and if I copy the path and paste it into a web browser, the jpeg image is displayed as expected.
I'm guessing this is some kind of permission problem that exists to prevent applications from accessing the file system directly? I had a similar problem when attempting to open files selected from an Open Panel, which turned out to be a problem with running Open/Save Panels from sandboxes, so I turned off sandboxing to get that aspect of my application working.
Does this ring a bell for anyone? I'm kind of baffled...
If you want to create an NSURL using fileURLWithPath then you need to provide a path, not a URL. Remove the use of file:.
NSString *path = #"/Users/jpurlia/Documents/Development/Test/1915brsts880804of.jpg";
NSURL *url = [NSURL fileURLWithPath:path];
Or you can fix the file URL and use NSURL URLWithString. Use file:// before the absolute file path so you have 3 /:
NSString *fileURLString = #"file:///Users/jpurlia/Documents/Development/Test/1915brsts880804of.jpg";
NSURL *url = [NSURL URLWithString:fileURLString];

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

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.

Why does NSMutableURLRequest instantiation cause a crash?

I am trying to create a NSMutableURLRequest like this:
NSURL *URLWithString = [
NSString stringWithFormat:#"%#?%#",
urlString,
datas
];
NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:URLWithString] autorelease];
When I run it on my iPhone 4S, the app crashes and I get the following exception:
2012-10-30 15:58:53.495 [429:907] -[__NSCFString absoluteURL]:
unrecognized selector sent to instance 0x1cd74a90
2012-10-30 15:58:53.497 [429:907] --- Terminating app due to uncaught
exception 'NSInvalidArgumentException', reason: '-[__NSCFString
absoluteURL]: unrecognized selector sent to instance 0x1cd74a90'
--- First throw call stack:
(0x361b62a3 0x344c697f 0x361b9e07 0x361b8531 0x3610ff68 0x3611363f
0x320396e7 0x32039551 0x320394ed 0x33bde661 0x33bde597 0x387e1
0x376d9f1f 0x376da9a9 0x341c535d 0x3618b173 0x3618b117 0x36189f99
0x360fcebd 0x360fcd49 0x366392eb 0x374db301 0x37cc1 0x37c58)
libc++abi.dylib: terminate called throwing an exception
What's wrong?
Lots of issues:
For a start look into how to invoke NSString and NSURL methods. I have done it for the code you have pasted.
NSURL * myUrl = [NSURL URLWithString:[NSString stringWithFormat:#"%#?%#",urlString,datas]];
and
NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:myUrl] autorelease];
Your NSURL creation code is wrong.
NSURL *URLWithString = [NSString stringWithFormat: #"%#?%#", urlString, datas];
Here, you are trying to create an NSURL directly with an NSString class method (stringWithFormat). The result is that your variable URLWithString will be of the wrong type, and when you send it an NSURL message you'll get a crash like you are getting.
To fix this you need to create an NSString of the URL address first, and instantiate an NSURL using that, like so:
NSString *completeURLString = [NSString stringWithFormat:#"%#?%#", urlString, datas];
NSURL *completeURL = [NSURL URLWithString: completeURLString];
(Technically, these two lines can be combined; I've separated them to make it clear what's going on.)
Also, whilst probably not related to your crash, you shouldn't call your URL variable URLWithString, as that is the name for a class method of NSURL. Make sure to come up with a unique name for it, and to start it with a lower case character (at the very least, this will make your code easier to decipher for others).

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?