I am writing a cocoa application in which I want to download a file from a webserver. What will be the most convenient method to go about doing this? Should I go in for NSSockets or a NSUrlRequest? Or is there any other easier way to achieve this?
我正在编写一个cocoa应用程序,我想从Web服务器下载文件。这样做最方便的方法是什么?我应该进入NSSockets还是NSUrlRequest?或者还有其他更简单的方法来实现这一目标吗?
6 个解决方案
#1
If you want to load the contents of the file into memory, many of the Cocoa data classes such as NSString, NSData and even NSDictionary have initWithURL:
methods, which initialize directly with the contents of a web request. They're very easy to use, but they're not very flexible or provide for good error handling. NSURLConnection provides a more flexible way to load data if you need it.
如果要将文件内容加载到内存中,许多Cocoa数据类(如NSString,NSData甚至NSDictionary)都具有initWithURL:方法,这些方法直接使用Web请求的内容进行初始化。它们非常易于使用,但它们不是非常灵活或提供良好的错误处理。如果需要,NSURLConnection可以更灵活地加载数据。
If you want to download the file directly to disk, then NSURLDownload would be the best bet.
如果要将文件直接下载到磁盘,那么NSURLDownload将是最好的选择。
#2
A word of warning: The initWithURL: methods are blocking, which is a big problem if the file is large, the server is slow, the user's internet connection is slow, etc. Don't call them from the main thread.
一句警告:initWithURL:方法是阻塞的,如果文件很大,服务器很慢,用户的互联网连接速度慢等,这是一个很大的问题。不要从主线程调用它们。
You also don't get any progress reporting, so when the download is slow, you have no way to tell the user how far along it is or how much longer it will take.
您也没有获得任何进度报告,因此当下载速度很慢时,您无法告诉用户它的走分程度或需要多长时间。
In almost all cases, you should use NSURLDownload or NSURLConnection instead.
几乎在所有情况下,您都应该使用NSURLDownload或NSURLConnection。
#3
The simplest thing to do is probably use NSURLDownload with NSURLRequest.
最简单的方法是使用NSURLownload与NSURLRequest。
#4
NSURLConnection is good if you want to get data from the web service into an NSString or NSData. Make sure you make asynchronous calls and handle errors and data in the NSURLConnection methods
如果要将Web服务中的数据转换为NSString或NSData,NSURLConnection是很好的。确保在NSURLConnection方法中进行异步调用并处理错误和数据
Here's a good example for REST-style calls http://kosmaczewski.net/2008/03/26/playing-with-http-libraries/
这是REST风格调用的一个很好的例子http://kosmaczewski.net/2008/03/26/playing-with-http-libraries/
#5
NSURLConnection does give you the most granularity, but be careful with NSURLConnection's sendSynchronousRequest() method. It leaks memory each time (have attached the XCode Leak Instrumentation tool and run it to prove it to myself) and gives weird HTTP 204 responses for no reason at all on occasion. I've blogged about this here
NSURLConnection确实为您提供了最大的粒度,但请注意NSURLConnection的sendSynchronousRequest()方法。它每次泄漏内存(附加了XCode Leak Instrumentation工具并运行它来向我自己证明)并且偶尔会毫无理由地提供奇怪的HTTP 204响应。我在这里写了这篇博文
#6
And another way is using libcurl, which comes preinstalled on any OS X system. You'd better make sure System Settings like proxies etc. are respected though if you use this approach.
另一种方法是使用libcurl,它预装在任何OS X系统上。如果使用这种方法,最好确保代理等系统设置得到尊重。
#1
If you want to load the contents of the file into memory, many of the Cocoa data classes such as NSString, NSData and even NSDictionary have initWithURL:
methods, which initialize directly with the contents of a web request. They're very easy to use, but they're not very flexible or provide for good error handling. NSURLConnection provides a more flexible way to load data if you need it.
如果要将文件内容加载到内存中,许多Cocoa数据类(如NSString,NSData甚至NSDictionary)都具有initWithURL:方法,这些方法直接使用Web请求的内容进行初始化。它们非常易于使用,但它们不是非常灵活或提供良好的错误处理。如果需要,NSURLConnection可以更灵活地加载数据。
If you want to download the file directly to disk, then NSURLDownload would be the best bet.
如果要将文件直接下载到磁盘,那么NSURLDownload将是最好的选择。
#2
A word of warning: The initWithURL: methods are blocking, which is a big problem if the file is large, the server is slow, the user's internet connection is slow, etc. Don't call them from the main thread.
一句警告:initWithURL:方法是阻塞的,如果文件很大,服务器很慢,用户的互联网连接速度慢等,这是一个很大的问题。不要从主线程调用它们。
You also don't get any progress reporting, so when the download is slow, you have no way to tell the user how far along it is or how much longer it will take.
您也没有获得任何进度报告,因此当下载速度很慢时,您无法告诉用户它的走分程度或需要多长时间。
In almost all cases, you should use NSURLDownload or NSURLConnection instead.
几乎在所有情况下,您都应该使用NSURLDownload或NSURLConnection。
#3
The simplest thing to do is probably use NSURLDownload with NSURLRequest.
最简单的方法是使用NSURLownload与NSURLRequest。
#4
NSURLConnection is good if you want to get data from the web service into an NSString or NSData. Make sure you make asynchronous calls and handle errors and data in the NSURLConnection methods
如果要将Web服务中的数据转换为NSString或NSData,NSURLConnection是很好的。确保在NSURLConnection方法中进行异步调用并处理错误和数据
Here's a good example for REST-style calls http://kosmaczewski.net/2008/03/26/playing-with-http-libraries/
这是REST风格调用的一个很好的例子http://kosmaczewski.net/2008/03/26/playing-with-http-libraries/
#5
NSURLConnection does give you the most granularity, but be careful with NSURLConnection's sendSynchronousRequest() method. It leaks memory each time (have attached the XCode Leak Instrumentation tool and run it to prove it to myself) and gives weird HTTP 204 responses for no reason at all on occasion. I've blogged about this here
NSURLConnection确实为您提供了最大的粒度,但请注意NSURLConnection的sendSynchronousRequest()方法。它每次泄漏内存(附加了XCode Leak Instrumentation工具并运行它来向我自己证明)并且偶尔会毫无理由地提供奇怪的HTTP 204响应。我在这里写了这篇博文
#6
And another way is using libcurl, which comes preinstalled on any OS X system. You'd better make sure System Settings like proxies etc. are respected though if you use this approach.
另一种方法是使用libcurl,它预装在任何OS X系统上。如果使用这种方法,最好确保代理等系统设置得到尊重。