nsurlsession

AFNetworking实现原理理解

我与影子孤独终老i 提交于 2019-12-01 04:42:51
NSURLSession: NSURLSession由三个基本模块构成: NSURLSession NSURLSessionConfiguation NSURLSessionTask NSURLSession相对于平时通信中的会话,但本身却不会进行网络数据传输,它会穿件多个NSURLSessionTask去执行每次的网络请求 NSURLSession的行为取决于三个方面。包括NSURLSession的类型、NSURLSessionTask的类型和在创建task时APP是否处于前端 NSURLSession有三种类型 defaultSession将cache和creditials储存于本地 Ephemeral Session对数据更加保密安全,并不会向本地储存任何数据,将cache和creditials储存在内存中,并和Session绑定,当Session销毁时,对应的数据也会被销毁。 backgroundSession可以时APP处于后台时继续数据传输,其行为与defaultSession类似,但是所有的数据传输均由一个非本APP的进程来管理。也有一些功能上的限制。 在创建Session对象时通过NSURLSessionConfigration来配置,可设置Session的delegate Session一但配置完成,就不能修改,除非创建一个新的Session对象。

ios NSURLSession简介

二次信任 提交于 2019-12-01 04:42:39
iOS7 中我们可以使用NSURLSession类来进行URL请求。如果是更早之前的版本使用NSURLConnection,在此之前先看下ios中关于URL的一些相关类。 URLLoading部分是我们主要使用的类,其它五个是帮助类。涉及各个方面。协议支持,加密,cookie管理,缓存,配置。 NSURLSession中的任务的行为依赖于三件事:session的类型(由传入的configuration对象来决定),task的类型,当task创建时应用是否处于前台。 Session的类型 1. 默认session使用diskcache并将证书保存在用户keychain中。 2. 短暂session没有将任何数据存在disk上,所有相关的东西都在ram中,当session无效时,这些内容自动被清除。 3.后台session,和默认session类似,除了独立一个进程来处理所有的数据传送。还有些限制。 Task的类型 在一个session中,NSURLSession支持三种任务类型:data tasks, download tasks, upload tasks data task: 使用NSData来发送和接收数据,适用于频繁的,数据量很小的交互。可以在数据到达时将数据发送给应用,或是在数据接收完成时,回调。 download task:以文件的形式接收数据。并且当app没有运行时

对于AlamoFire几点思考

只愿长相守 提交于 2019-12-01 04:42:18
本文由 CocoaChina --BYB_1132( 论坛ID )翻译 原文: Thoughts On AlamoFire--Swift’s AFNetworking Implementation HTTP协议就是现代开发的同义词,对于有经验的iOS开发者来说, 熟悉并尽可能使用这些流行的协议是日常工作的基础。 不出意料,iOS 应用在这一点上没有什么不同,成千上万的app和工程师都依靠广受欢迎的AFNetworking 库实现与服务器的交互,JSON解析, 以及提供占位符图片等多个功能。 简言之,做到这些并不容易。这篇文章中我们了要解的是Alamofire库。 基础 Alamofire的核心主要是试图简化iOS中HTTP网络连接, 它通过使用NSURLSession以及Foundation URL Loading System来创建一个Swift本地的网络访问接口,从而实现令人难以置信效率的任务。 Swift放弃了代理模式取而代之的使用了回调。对于我而言,我喜欢这个选择。然而基础承诺机制模式( promise-based patterns )也能起到一定作用, 它们也隐藏了一些坏代码味道,对于一些人有点太神奇了。 进一步看,它是异步实现的。你也许听说过,但是 在主线程上执行网络调用并不是一个好主意 ,不过Alamofire采用了许多创造性的最优方法。 例如

ios FTP upload using NSURLSession

泪湿孤枕 提交于 2019-12-01 03:02:57
I am trying to upload files via FTP to server. According to Apple Documentation NSURLSession class supports FTP operations. There is a famous Apple Developer blog which also supports that. But still its not clear whether NSURLSession API's supports ftp upload or not? (I tried with the way suggested and getting error). With the conventional way of CFFTPStreamRef , ftp upload works fine but it's deprecated in 9.0. The header says: CF_DEPRECATED(10_3, 10_11, 2_0, 9_0 , "Use NSURLSessionAPI for ftp requests") Any idea, example or link to get help with. I am trying something like this for now:

HTTP digest authentication fail due to wrong nonce-count in iOS 10

假装没事ソ 提交于 2019-12-01 00:38:11
HTTP digest authentication no longer works in our app since iOS 10 due to wrong nonce-count in Authorization: Digest header generated by NSURLSession. The same code works in iOS 9, but fail to auth in iOS 10 Create a POST request with NSURLRequest Fire it with NSURLSession Handle NSURLAuthenticationMethodHTTPDigest in urlSession(_:didReceive:completionHandler:) delegate The server responds with a 401 and qop="auth" string as expected The app requests again with the Authorization: Digest header set. According to RFC2617 : nonce-count This MUST be specified if a qop directive is sent (see above)

Download a file with NSURLSession in Swift

我的梦境 提交于 2019-11-30 23:12:12
i have like 2 problems here , first i cant set NSURLSessionDownloadDelegate with a swift project, compiler says Type 'ViewController' does not conform to protocol 'NSURLSessionDownloadDelegate' Second problem is i cant find NSURLSession methods to download a simple file here is the way i use to download the simple file var url:NSURL = NSURL.URLWithString(fileURL) var request:NSURLRequest = NSURLRequest(URL: url) var downloadTask:NSURLSessionDownloadTask = sessionManager.downloadTaskWithRequest(request) downloadTask.resume() and these are the methods i want to make in swift URLSession:

How do I invalidate iOS's cache for a particular URL?

喜欢而已 提交于 2019-11-30 20:29:17
Using NSURLSession 's default caching, how do I invalidate the cache for a particular URL? I note NSURLCache 's removeCachedResponseForRequest: method, but that takes an NSURLRequest object, which I don't have for the original request. Do I need to store those as I create them so I can then pass them back into removeCachedResponseForRequest: or can I just create a new one with the appropriate URL which will then serve as equivalent for the purpose, even if it doesn't have the same header fields and other properties as the original? If you want to go further you could reset the cached response

Does NSURLSession Take place in a separate thread?

╄→гoц情女王★ 提交于 2019-11-30 18:58:14
I was designing an app that uses NSURLSession and thinking about putting it in a different thread with Grand Central Dispatch, but if NSURLSession automatically does that in the background, I wouldn't have to use GCD then, correct? So in other words, does NSURLSession automatically use Grand Central Dispatch in the background, so we don't have to worry about it? Yes, NSURLSession does its work in a background thread. The download ALWAYS takes place on a background thread. EDIT: There is no reason to wrap your code that invokes NSURLSession (or URLSession in Swift 3/Swift 4) in a GCD call. You

NSURLSessionDataTask not executing the completion handler block

那年仲夏 提交于 2019-11-30 18:52:52
I am trying to pull some data from my local node server. The server is getting the get request and logging it, but for some reason my iOS app will not execute any of the code that I have in the completion handler. Here is the code: - (IBAction) buttonPressed{ NSURL *url = [NSURL URLWithString:@"http://127.0.0.1:3000/"]; NSURLSessionDataTask *dataTask = [self.session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){ nameLabel.text = @"yay!"; /* if (!error){ nameLabel.text = @"noerr"; NSHTTPURLResponse *httpResp = (NSHTTPURLResponse *)response; if

Download a file with NSURLSession in Swift

老子叫甜甜 提交于 2019-11-30 18:12:38
问题 i have like 2 problems here , first i cant set NSURLSessionDownloadDelegate with a swift project, compiler says Type 'ViewController' does not conform to protocol 'NSURLSessionDownloadDelegate' Second problem is i cant find NSURLSession methods to download a simple file here is the way i use to download the simple file var url:NSURL = NSURL.URLWithString(fileURL) var request:NSURLRequest = NSURLRequest(URL: url) var downloadTask:NSURLSessionDownloadTask = sessionManager