51

I am trying to access an XML file store in the resources directory. I am using NSURL to access this file using NSURLConnection( this file is going to be swapped out for a remote service in the future).

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL 
URLWithString:@"file:///XMLTest.xml"] 
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
    // Create the NSMutableData to hold the received data.
    // receivedData is an instance variable declared elsewhere.
    response = [[NSMutableData data] retain];
} else {
    NSLog(@"Could not create connection");
}

The class that starts the connection implements the NSURLConnection methods:

connection:willCacheResponse: 
connection:didReceiveData: 
connectionDidFinishLoading: 
connectionDidFinishLoading:

Once I launch this the simulator dies, no messages are printed to the console so I am at a loss for where to look. Any ideas, or am I just doing this completely wrong?

botptr
  • 987
  • 3
  • 11
  • 18

5 Answers5

159

Trying to load anything from the filesystem root is wrong, wrong, wrong. Definitely wrong on the device, and probably wrong on the simulator. The resources directory should be accessed via the NSBundle class.

For example, to get a URL for a file called "Data.txt" in the resources, use the following:

NSURL *MyURL = [[NSBundle mainBundle]
    URLForResource: @"Data" withExtension:@"txt"];
Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • 1
    Agreed. This is the correct way of accessing the resources folder! – Nick Cartwright Feb 10 '11 at 17:26
  • 2
    I was under the impression '/' on iOS accessed the application sandbox root, I could easily be very wrong. Here is the code I have used to get the url, application still blows up, but this seems like a much smarter move. ` NSURL *url = [[NSBundle mainBundle] URLForResource:@"XMLTest" withExtension:@"xml"];` – botptr Feb 10 '11 at 18:58
  • 1
    On the simulator, it seems, / means the host computer root. Meaning you'll get wildly different results on device and on simulator - not good. And anyway, the sandbox root is not the same as the bundle resources folder. – Seva Alekseyev Feb 10 '11 at 19:10
  • Could you expand your answer by providing a little more code? I'm doing to following but its not working: `NSURL *bundleResourcePath = [[NSBundle mainBundle] resourceURL];` `NSURL *url = (NSURL *) [[NSBundle mainBundle] pathForResource:@"SomeImage" ofType:@"png" inDirectory:[NSString stringWithContentsOfURL:bundleResourcePath encoding:NSStringEncodingConversionExternalRepresentation error:nil]];` `NSLog(@"bundleResourcePath: %@", bundleResourcePath);` `NSLog(@"url: %@", url);`. The first NSLog shows the URL correctly however the second outputs (null). – ElasticThoughts Apr 24 '12 at 17:46
  • @CocoaNoob: edited. But you seem to be confused between paths and URLs. – Seva Alekseyev Apr 24 '12 at 18:01
34

If you want to get a URL from a path (say, because you created a file in NSTemporaryDirectory() and you need to get that as a URL) you can easily do so by using NSURL's fileURLWithPath method:

NSString* tempPath = NSTemporaryDirectory();
NSString* tempFile = [tempPath stringByAppendingPathComponent:fileName];
NSURL* URL = [NSURL fileURLWithPath:tempFile];

Much easier than +URLWithString: and other methods.

Andres Kievsky
  • 3,461
  • 32
  • 25
21

This would also work:

Obj-C

NSString *path = [[NSBundle mainBundle] pathForResource:@"XMLTest" ofType:@"xml"];

Swift 5

let url = Bundle.main.url(forResource: "XMLTest", withExtension: "xml")!

Hope this helps!

Zorayr
  • 23,770
  • 8
  • 136
  • 129
Nick Cartwright
  • 8,334
  • 15
  • 45
  • 56
  • 1
    This one gives filesystem path, not URL. I don't understand why, but the OP wants to load his XML from URL. Maybe for easier porting to web-based sources... – Seva Alekseyev Feb 10 '11 at 17:36
  • 11
    Although my answer has been marked as the correct answer, I believe the answer from Seva is actually correct! Can this be updated? – Nick Cartwright Mar 09 '11 at 13:19
  • Where does the file need to be located in the folder hierarchy for this to work? – Zorayr Aug 26 '20 at 23:26
2

Swift 3:

    let myFileName = "somefilename"
    let myURL = Bundle.main.url(forResource: myFileName, withExtension: "png") 
Gokila Dorai
  • 100
  • 5
2

You can try this

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"file://localhost/Users/userName/Desktop/XMLTest.xml"]];

here assuming file is in desktop.

Abdullah Md. Zubair
  • 3,312
  • 2
  • 30
  • 39