NSURL is null in Simulator, but okay on iPad - objective-c

I'm trying to load an audio file into AVAudioPlayer on the iPad. When I run it on the iPad it finds it in the bundle fine. However, if I try and run it through the simulator, I get a null error for NSURL. Here's the snippet of code (num is an arbitrary int):
NSString *name = [NSString stringWithFormat:#"st-answermachine-%i", num];
NSLog(#"name = %#", name);
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:#"m4a"];
NSLog(#"path = %#", path);
NSURL *url = [NSURL URLWithString:path];
NSLog(#"url = %#", url);
In the simluator, the Debugger Console traces this:
name = st-answermachine-1
path = /Users/joe/Library/Application Support/iPhone Simulator/3.2/Applications/B85E9CC8-6E39-47B9-XXXX-1E3A2CE145D1/MyApp.app/st-answermachine-1.m4a
url = (null)
But if I try it on the device, I get this:
name = st-answermachine-1
path = /var/mobile/Applications/116DA1CB-EA13-4B80-XXXX-EBD46C8E2095/MyApp.app/st-answermachine-1.m4a
url = /var/mobile/Applications/116DA1CB-EA13-4B80-XXXX-EBD46C8E2095/MyApp.app/st-answermachine-1.m4a
Any ideas why I might have this problem please?
Thanks!

URLWithString: expects a string containing an actual URL as its parameter (e.g. 'http://blah/' or 'file:///blah'). URLs can't contain spaces (as the simulator's path does), and that's why it's failing.
As Evan suggests, you need to use fileURLWithPath: to convert a path string to a URL object.

Related

NSBundle crash when setting string name as number?

I am trying to play some sound, that its name is a number . so i create an NSString as number, and when i try to set it to the NSBundle, it is Null!
It does work with a word, such as #"yes" , as the name parameter
//name parameter only works as a string in words. when its a number it doesn't.
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:name ofType:type];
NSLog(#"%#",name); //logs 16 !
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; //crash!
the crash is because the soundFilePath is nil .
NSString *number=[NSString stringWithFormat:#"%d",16];
Ok. Problem is that i had to add the sounds into the build phase-copy bundle resources, so he can find them.

Parsing NSURL in Objective-C

I have an NSTextView control that could potentially have links in it. How do I get the full url of the link?
Here is what I have so far
-(BOOL)textView:(NSTextView *)aTextView clickedOnLink:(id)aLink atIndex:(NSUInteger)charIndex
{
NSURL *htmlURL = [NSURL fileURLWithPathComponents:[aLink pathComponents]];
}
This gives me a URL that begins with file://localhost ... How do I get rid of that portion of the URL?
NSURL* url = [NSURL URLWithString:#"http://localhost/myweb/index.html"];
NSString* reducedUrl = [NSString stringWithFormat:
#"%#://%#",
url.scheme,
[url.pathComponents objectAtIndex:1]];

Drag&Drop NSURL without "file://"

I'm implementing my drag&drop method. I need that when user drags something on my app window I can get that file URL. NSURL needs to be converted to char. Thats OK. But how to remove file:// from url? My current code:
pboard = [sender draggingPasteboard];
NSString *url = [[NSURL URLFromPasteboard:pboard] absoluteString];
input_imageN = strdup([url UTF8String]);
its OK, but it gives url with file:// prefix. I tried using
NSURL *fileUrl = [[NSURL URLFromPasteboard:pboard] isFileURL];
NSString *url = [fileUrl absoluteString];
NSLog(#"url: %#", [NSURL URLFromPasteboard:pboard]);
input_imageN = strdup([url UTF8String]);
but it says that
Cannot initialize a variable of type 'NSURL *' with an rvalue of type 'BOOL' (aka 'signed char')
at
NSURL *fileUrl = [[NSURL URLFromPasteboard:pboard] isFileURL];
To go from a file URL to the path as a C string in the appropriate representation for the filesystem, you'd do:
NSURL *fileURL = [NSURL URLFromPasteboard: pboard];
NSString *filePath = [fileURL path];
char *filesystemRepresentation = [filePath filesystemRepresentation];
This avoids assumptions that stripping off the scheme leaves you with just the path, or that the filesystem is definitely happy accepting UTF8-encoded paths.
url = [url stringByReplacingOccurencesOfString:#"file://" withString:#""];
Hope this helps. Cheers!
#user23743's answer is correct. Since iOS 7 though NSURL has its own filestSystemRepresentation method.
In Swift:
if let fileURL = NSURL(fromPasteboard: pboard) {
let representation = fileURL.fileSystemRepresentation
}
if let fileURL = NSURL(from: pboard)?.filePathURL {
}
has been most effective for me.

Problem in converting urlString into NSURL in iphone sdk

I am having string at first the method calls with timestamp value nil and I am getting converted the string into url .next time when I click load more results button again the method calls with time stamp value assigned to it.but the url string is not converting into NSURL iam getting the null value into it.
-(NSMutableArray*)getTextMessagesArray:(NSString *)endTimestamp
{
printf("\n endtimestamp value...%s",[endTimestamp UTF8String]);
NSString *urlString = #"http://123.237.186.221:8080/upload/textRequest.jsp";
urlString = [urlString stringByAppendingString:#"?beginTimestamp="];
urlString = [urlString stringByAppendingString:#"&endTimestamp="];
if([endTimestamp length]>0)
{
urlString = [urlString stringByAppendingString:endTimestamp];
}
printf("\n &*(*(((urlString...%s",[urlString UTF8String]);
NSURL* aUrl = [NSURL URLWithString:urlString];
NSLog(#"url in appdelegaare in text...%#",aUrl);
[textParser parseXMLFileAtURL:aUrl];
textMessagesList = [textParser getTextMessagesList];
printf("\n textMessagesList Count in appDelegate....%d",[textMessagesList count]);
return textMessagesList;
}
The result I am getting in console is:
&*(*(((urlString...http://123.237.186.221:8080/upload/textRequest.jsp?endTimestamp=2010-10-08 16:20:47.0
url in appdelegaare in text...(null)
Guy's can any one suggest me why this happening
Anyone's help will be much appreciated.
Thanks to all,
Monish.
Your problem is that valid URLs cannot contains spaces. You want to do something along the following lines:
NSString *escapedUrlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// escapedUrlString should be "http://123.237.186.221:8080/upload/textRequest.jsp?endTimestamp=2010-10-08%2016:20:47.0"
NSURL *aUrl = [NSURL URLWithString:escapedUrlString];
This might be what you wanted.

How to download file from particular url?

NSURL * url = #"http://192.168.100.161/UploadWhiteB/wh.txt";
NSData * data = [NSData dataWithContentsOfURL:url];
if (data != nil) {
NSLog(#"\nis not nil");
NSString *readdata = [[NSString alloc] initWithContentsOfURL:(NSData *)data ];
I write this code to download a file from given url... but i get an error on line
NSData * data = [NSData dataWithContentsOfURL:url];
uncaught exception...
so please help me out.
Your first line should be
NSURL * url = [NSURL URLWithString:#"http://192.168.100.161/UploadWhiteB/wh.txt"];
(NSURL is not a string, but can easily be constructed from one.)
I'd expect you to get a compiler warning on your first line--ignoring compiler warnings is bad. The second line fails because dataWithContentsOfURL: expects to be given a pointer to an NSURL object and while you're passing it a pointer that you've typed NSURL*, url is actually pointing to an NSString object.
NSString *file = #"http://192.168.100.161/UploadWhiteB/wh.txt";
NSURL *fileURL = [NSURL URLWithString:file];
NSLog(#"qqqqq.....%#",fileURL);
NSData *fileData = [[NSData alloc] initWithContentsOfURL:fileURL];
-[NSString initWithContentsOfURL:] is deprecated. You should be using -[NSString (id)initWithContentsOfURL:encoding:error:]. In either case, the URL paramter is an NSURL instance, not an NSData instance. Of course you get an error trying to initialize a string with the wrong type. You can initialize the string with the URL data using -[NSString initWithData:encoding:], or just initialize the string directly from the URL.