アプリバンドルでファイルへのパス
-
20-12-2019 - |
質問
私は何度も何度もしましたが、それは失敗します。 私は私のアプリ内に出荷されたファイルにアクセスしたいです。
NSString* path = [[NSBundle mainBundle] pathForResource:@"information" ofType:@"xml"];
.
を返します
/Users/eldude/Library/Application Support/iPhone Simulator/7.0.3/Applications/5969FF96-7023-4859-90C0-D4D03D25998D/App.app/information.xml
.
これは端末とそれすべてのものでチェックされています。ただし、パスを解析しようとして失敗する
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;
}
.
Error Domain=NSCocoaErrorDomain Code=-1 "The operation couldn’t be completed. (Cocoa error -1.)" UserInfo=0xb9256f0 {NSXMLParserErrorMessage=Could not open data stream}
.
アイデア?
解決
ファイルのURLとネットワークURLは異なります。
アップルのマニュアルから:
重要:ファイルシステムパス用のNSURLオブジェクトを作成するには、
fileURLWithPath:isDirectory:
またはただ
fileURLWithPath:
例:
NSString *filePath = @"path/file.txt";
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
NSLog(@"fileURL: %@", [fileURL absoluteURL]);
NSURL *netURL = [NSURL URLWithString:filePath];
NSLog(@"netURL: %@", [netURL absoluteURL]);
.
nslog出力:
fileURL:file:///path/file.txt
NetURL:パス/ file.txt
所属していません StackOverflow