Accessing Local file using NSURL

IphoneIos

Iphone Problem Overview


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?

Iphone Solutions


Solution 1 - Iphone

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

Solution 2 - Iphone

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.

Solution 3 - Iphone

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!

Solution 4 - Iphone

You can try this

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

here assuming file is in desktop.

Solution 5 - Iphone

Swift 3:

    let myFileName = "somefilename"
    let myURL = Bundle.main.url(forResource: myFileName, withExtension: "png") 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbotptrView Question on Stackoverflow
Solution 1 - IphoneSeva AlekseyevView Answer on Stackoverflow
Solution 2 - IphoneAndres KievskyView Answer on Stackoverflow
Solution 3 - IphoneNick CartwrightView Answer on Stackoverflow
Solution 4 - IphoneAbdullah Md. ZubairView Answer on Stackoverflow
Solution 5 - IphoneGokila DoraiView Answer on Stackoverflow