ios - NSURLConnection、NSURLSession 和 AFNetworking 有什么区别?

标签 ios

<分区>

我想知道它们之间的基本区别是什么,以及在 iOS 中以 Objective-c 和 swift 结尾管理 Web 服务的最佳和常用方法是什么。

之前我在 Objective-c 工作,当我们需要使用 web 服务时,我们使用 AFNetworking 和 NSURLConnections,随着 NSURLSession 的启动,我迅速开始使用这个。 那么哪个合适,这样我就可以管理所有 web 服务网络连接条件等?

最佳答案

NSURLConnectionNSURLSession的区别

The entire model is different. NSURLSession is designed around the assumption that you'll have a lot of requests that need similar configuration (standard sets of headers, etc.), and makes life much easier if you do.

NSURLSession also provides support for background downloads, which make it possible to continue downloading resources while your app isn't running (or when it is in the background on iOS). For some use cases, this is also a major win.

NSURLSession also provide a grouping of related requests, making it easy to cancel all of the requests associated with a particular work unit, such as canceling all loads associated with loading a web page when the user closes the window or tab.

NSURLSession also provides nicer interfaces for requesting data using blocks, in that it allows you to combine them with delegate methods for doing custom authentication handling, redirect handling, etc., whereas with NSURLConnection if you suddenly realized you needed to do those things, you had to refactor your code to not use block-based callbacks.

NSURLConnectionNSURLSession 和 AFNetworking 的区别?

NSURLConnection and NSURLRequest are the provided Cocoa classes for managing connections. In iOS 7, Apple added NSURLSession.

But I think you'll find AFNetworking to be a framework that further simplifies network requests (notably complex HTTP requests). If you don't want to use third-party frameworks for any reason, you can use NSURLConnection and/or NSURLSession directly. It just takes a little more coding.

For information on NSURLConnection and NSURLSession see the URL Loading System Programming Guide.

Find from reference1 and reference2

谢谢robdgatwood对于这样一个惊人的答案...

关于ios - NSURLConnection、NSURLSession 和 AFNetworking 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40542118/

相关文章:

ios - 如果在 iOS10 中请求对本地通知的授权时发生错误,authorizationStatus 是什么?

ios - slider 改变 UIColor 的亮度

iphone - xcode5 ios7错误编译代码

ios - NSLocationWhenInUseUsageDescription 德语翻译不起作用

ios - iPhone : Using localization change language of an app set it in NSUserDefaults

iOS持久化: storing and retrieving items in directory

ios - 自动布局:UIImageView 和 UIButton 之间的区别

ios - 更改 iOS 应用本地化运行时

ios - 使用WKWebView读取本地存储数据

ios - 如何从 iOS 连接到本地 Google App Engine 端点?