使用NSURL访问本地文件 [英] Accessing Local file using NSURL

查看:94
本文介绍了使用NSURL访问本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问资源目录中的XML文件存储。我正在使用NSURL使用NSURLConnection访问此文件(此文件将来将被替换为远程服务)。

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");
}

启动连接的类实现了NSURLConnection方法:

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?

推荐答案

这也有效:

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

希望这有帮助!

这篇关于使用NSURL访问本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆