ios 使用 NSURL 访问本地文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4960375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:51:34  来源:igfitidea点击:

Accessing Local file using NSURL

iphoneios

提问by botptr

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).

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

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:

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

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?

一旦我启动它,模拟器就会死机,控制台上不会打印任何消息,所以我不知道在哪里看。任何想法,或者我只是这样做完全错误?

采纳答案by Nick Cartwright

This would also work:

这也可以:

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

Hope this helps!

希望这可以帮助!

回答by Seva Alekseyev

Trying to load anything from the filesystem rootis wrong, wrong, wrong. Definitely wrong on the device, and probably wrong on the simulator. The resources directory should be accessed via the NSBundleclass.

试图从文件系统根目录加载任何东西是错误的,错误的,错误的。在设备上肯定是错误的,在模拟器上也可能是错误的。资源目录应该通过NSBundle类访问。

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

例如,要获取资源中名为“Data.txt”的文件的 URL,请使用以下命令:

NSURL *MyURL = [[NSBundle mainBundle]
    URLForResource: @"Data" withExtension:@"txt"];

回答by Andres Kievsky

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:

如果您想从路径中获取 URL(例如,因为您在 NSTemporaryDirectory() 中创建了一个文件,并且需要将其作为 URL 获取),您可以使用 NSURL 的 fileURLWithPath 方法轻松实现:

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

Much easier than +URLWithString: and other methods.

比 +URLWithString: 和其他方法容易得多。

回答by Abdullah Md. Zubair

You can try this

你可以试试这个

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

here assuming file is in desktop.

这里假设文件在桌面上。

回答by Gokila Dorai

Swift 3:

斯威夫特 3:

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