I did this many times before but now it fails. I want to access a file shipped within my app.
NSString* path = [[NSBundle mainBundle] pathForResource:@"information" ofType:@"xml"];
returns
/Users/eldude/Library/Application Support/iPhone Simulator/7.0.3/Applications/5969FF96-7023-4859-90C0-D4D03D25998D/App.app/information.xml
which is correct - checked in terminal and all that. However, trying to parse the path fails
NSURL* fileURL = [NSURL URLWithString:path];
NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithContentsOfURL:fileURL];
[nsXmlParser setDelegate:self];
if(![nsXmlParser parse]){
NSError* e = nsXmlParser.parserError;
NSLog(@"ERROR parsing XML file: \n %@",e);
self = NULL;
}
with
Error Domain=NSCocoaErrorDomain Code=-1 "The operation couldn’t be completed. (Cocoa error -1.)" UserInfo=0xb9256f0 {NSXMLParserErrorMessage=Could not open data stream}
Ideas anyone?
File URLs and network URLs are different.
From the Apple documentation:
Important: To create NSURL objects for file system paths, usefileURLWithPath:isDirectory:
or justfileURLWithPath:
Example:
NSString *filePath = @"path/file.txt";
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
NSLog(@"fileURL: %@", [fileURL absoluteURL]);
NSURL *netURL = [NSURL URLWithString:filePath];
NSLog(@"netURL: %@", [netURL absoluteURL]);
NSLog Output:
fileURL: file:///path/file.txt
netURL: path/file.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With