NSURL fileURLWithPath where NSString has a space - objective-c

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?

Related

confused by substring result of stringByDeletingLastPathComponent

my code
NSMutableString *s= (NSMutableString *)[#"http://www.yahoo.com/index.html" stringByDeletingLastPathComponent];
what I expected result of s is
http://www.yahoo.com
but the code above show s is:
http:/www.yahoo.com
Your comment welcome
You should use NSURL, not NSString:
NSURL *url = [[NSURL URLWithString:#"http://www.yahoo.com/index.html"] URLByDeletingLastPathComponent];
Yielding:
http://www.yahoo.com/
If you absolutely need a string from this, you can then do:
NSString *urlString = url.absoluteString;
Or, if you really needed a mutable string, don’t cast it to NSMutableString, but do create a mutable copy:
NSMutableString *urlString = [url.absoluteString mutableCopy];
But, in general, where possible, you should stay with NSURL when dealing with URLs. And when tempted to use file paths, use file URLs instead.
The annotation for this method is explained as follows。
Apple Document: Note that this method only works with the file paths (not, for example, the string representations of URLs).
But you string is clearly a full URL address, does not belong to the file path, so we will assemble them into a URL, using URL classification URLByDeletingLastPathComponent to intercept
NSString *urlString = #"http://www.yahoo.com/index.html";
NSURL* URL = [NSURL URLWithString: urlString];
NSURL* lastPathUrl = [URL URLByDeletingLastPathComponent];
NSString* lastPathString = lastPathUrl.absoluteString;
NSLog(#"---%#---", lastPathString);

Issues playing local files with AVPlayer?

Basically, I'm writing a video file to the Application Support directory and then saving it's path:
NSString *guid = [[NSUUID new] UUIDString];
NSString *outputFile = [NSString stringWithFormat:#"video_%#.mp4", guid];
NSString *outputDirectory = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *tempPath = [outputDirectory stringByAppendingPathComponent:outputFile];
NSURL *fileURL = [NSURL fileURLWithPath:tempPath]
// code to write a video to fileURL
I save the string path itself by calling [fileURL path];
Now later when I try to create an AVAssetItem from it, I can't actually get it to play.
EDIT:
Ok, so it seems the issue is the space in Application Support. I tried saving the file to just the Library directory and it worked fine.
So the question becomes, how can I play a video I save in the Application Support directory. I wasn't able to create a valid NSURL without escaping the space (when I tried it would return a nil NSURL) but it seems that the space/escaping doesn't allow it to play correctly.
Assume the NSURL to NSString conversion is required (unless it really should be avoided).
Also, side note, but if anyone could give some insight as to why this question was down voted so I can improve the quality of my questions, that would be appreciated. I don't understand?
While I am not informed enough to opine about whether this would change if I had used matt's recommended methods: URLForDirectory:inDomain:appropriateForURL:create:error: and URLByAppendingPathComponent: the actual issue here isn't converting an NSURL to NSString
It's that I'm saving the full URL ([fileURL path]). This is because the [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0]; dynamically changes and the full path won't always point me to the appropriate file.
Instead I need to save just the name of my file (in my case I needed to persist outputFile) and then later dynamically build the the full path when I need it.
The same exact process:
NSString *outputDirectory = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *tempPath = [outputDirectory stringByAppendingPathComponent:outputFile];
NSURL *fileURL = [NSURL fileURLWithPath:tempPath];
worked just fine.
TLDR: Saving a full path doesn't work

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.

Declare an NSString With Format Specifiers and use it as a URL to open in UIWebview

I have an int containing a number. I am wanting to declare an NSString so I can use use format specifiers when assigning a value to it.
I thought it might be something like this:
NSString[NSString stringWithFormat] myString;
myString = [#"http://myurl.com/%d",myInt];
I gather this is not the case, so question one is: How do I declare an NSString that can handle format specifiers and then assign it a value using format specifiers? The purpose of this NSString is to hold a URL, exactly like the second line above.
Question two is, How do I then use this string as a URL to open in a UIWebView?
I assume I use something like this:
[webView loadRequest:
Sadly, this is as far as my knowledge stretches. Is there a way I can tell my UIWebView (webView above) to use the NSString with the URL I mentioned earlier?
I intend on having the NSString as a global variable, as it will be assigned it's value inside a C function. And 'webView' will use it inside a (what I think is a) method. All of this code is in the same file, the Delegate.m file. It is all executed on launch of the application.
Your string should look like this:
NSString *myString = [NSString stringWithFormat:#"http://myurl.com/%d", myInt];
What you missed: adding the * to indicate a pointer, and thinking that you had to/could first state that the string would have a format and then later state the format. It all happens at once, creating the string with the specified format.
Edited to add NSURL
To create a url you're creating an object of class NSURL, like this:
NSURL *myURL = [[NSURL alloc] initWithString:myString];
And then you create the url request:
NSURLRequest *request = [NSURLRequest requestWithURL:myURL];
And finally, tell your webView to load the request:
[webView loadRequest:request];
For your first part:
NSString *myString = [NSString stringWithFormat:#"http://myurl.com/%d", myInt];
Then, based on a tutorial from iphonesdkarticles.com:
//Create a URL object.
NSURL *url = [NSURL URLWithString:myString];
//URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];