使用 Xcode 下载文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10582704/
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
Downloading a file with Xcode
提问by Marcel Marino
I am new to Xcode, attempting to build my first iPad app. I need to write some code to download a file from the internet and save it to the hard drive on the iPad. I have found numerous suggestions on how to save images, or get text files then write them to the hard drive, or viewing .pdf's, but that's not what I'm looking for. I need code that will download any file once the correct URL is put in, and place it in a folder of my choosing. Anyone know how to do that? I would be greatly appreciative.
我是 Xcode 的新手,正在尝试构建我的第一个 iPad 应用程序。我需要编写一些代码来从 Internet 下载文件并将其保存到 iPad 的硬盘驱动器中。我找到了许多关于如何保存图像、获取文本文件然后将它们写入硬盘驱动器或查看 .pdf 的建议,但这不是我想要的。我需要在输入正确的 URL 后下载任何文件的代码,并将其放置在我选择的文件夹中。有谁知道怎么做?我将不胜感激。
回答by trojanfoe
This should get you started:
这应该让你开始:
NSString *stringURL = @"url to file";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
Blatantly stolen from this SO question.
从这个 SO 问题公然窃取。
BTW your subject shouldn't contain the word 'Xcode' as that is the IDE used to develop the App; it doesn't tell anyone anything about the App environment itself.
顺便说一句,您的主题不应包含“Xcode”一词,因为这是用于开发应用程序的 IDE;它不会告诉任何人有关 App 环境本身的任何信息。