nsurlsession

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

此生再无相见时 提交于 2019-11-30 05:32:42
问题 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

Does NSURLSession Take place in a separate thread?

自作多情 提交于 2019-11-30 03:21:42
问题 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? 回答1: Yes, NSURLSession does its work in a background thread. The download ALWAYS takes place on a background thread. EDIT: There is

NSURLSession with Token Authentication

て烟熏妆下的殇ゞ 提交于 2019-11-30 00:45:33
I have the following code in my iOS project and I want to convert to use NSURLSession instead of NSURLConnection . I am querying a REST API which uses a token-based HTTP Authentication scheme but I cannot find an example of how to do it. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]]; NSString *username = [[NSUserDefaults standardUserDefaults] stringForKey:@"Username"]; NSString *token = //GET THE TOKEN FROM THE KEYCHAIN NSString *authValue = [NSString stringWithFormat:@"Token %@",token]; [request setValue:authValue forHTTPHeaderField:@

NSURLSession学习笔记(二)Session Task

喜欢而已 提交于 2019-11-29 23:14:53
Session Task分为三种Data Task,Upload Task,Download Task。毫无疑问,Session Task是整个NSURLSession架构的核心目标。 下面写了一个简单的Demo来初步使用下三种任务对象。这里使用的是 convenience methods,并没有定制session和使用协议,都是采用completionHandler作为回调动作。 故事板内容为: 第一种Data Task用于加载数据,使用全局的shared session和dataTaskWithRequest:completionHandler:方法创建。代码如下: /* 使用NSURLSessionDataTask加载网页数据 */ - (IBAction)loadData:( id )sender { // 开始加载数据,让spinner转起来 [ self .spinner startAnimating ]; // 创建Data Task,用于打开我的csdn blog主页 NSURL *url = [NSURL URLWithString : @"http://blog.csdn.net/u010962810" ]; NSURLRequest *request = [NSURLRequest requestWithURL :url]; NSURLSession

NSURLSession学习笔记(三)Download Task

亡梦爱人 提交于 2019-11-29 23:14:21
NSURLSession的Download Task用于完成下载任务,本文介绍如何创建断点续传的下载任务和后台下载任务。 我们直接从分析Demo入手: 故事板如下: 只有一个View Controller,用于创建各种下载任务,并将下载后的图片显示到视图上,下载过程中会更新下载进度。 头文件代码如下: #import <UIKit/UIKit.h> @interface ViewController : UIViewController <NSURLSessionDownloadDelegate> /* NSURLSessions */ @property ( strong , nonatomic ) NSURLSession *currentSession; // 当前会话 @property ( strong , nonatomic , readonly ) NSURLSession *backgroundSession; // 后台会话 /* 下载任务 */ @property ( strong , nonatomic ) NSURLSessionDownloadTask *cancellableTask; // 可取消的下载任务 @property ( strong , nonatomic ) NSURLSessionDownloadTask *resumableTask;

[Swift]Swift网络开发之NSURLSession学习笔记

本小妞迷上赌 提交于 2019-11-29 23:14:09
Swift网络开发之NSURLSession学习笔记 先上效果图; 功能: -单个任务下载 -暂停下载任务 -取消下载任务 -断点下载 -显示下载进度及速度 -多任务下载 -分别控制各个任务 在如今移动互联网的浪潮中,手机APP越来越依赖网络通讯来交互数据。今天我们就来分享下如何通过使用NSURLSession这个Apple官方提供的网络接口实现文件下载的思路。 NSURLSsession 先来介绍下NSURLSession这个接口。NSURLSession是苹果在WWDC2013上推出的用于替代它的前辈NSURLConnection的。 与NSURLConnection类似,除了同名类NSURLSession,NSURLSession也是指一组相互依赖的类。NSURLSession包括与之前相同的组件,例如NSURLRequest, NSURLCache等。NSURLSession的不同之处在于,它把 NSURLConnection替换为NSURLSession, NSURLSessionConfiguration,以及3个NSURLSessionTask的子类:NSURLSessionDataTask, NSURLSessionUploadTask, 和NSURLSessionDownloadTask。 与NSURLConnection相比

How can I check that an NSData blob is valid as resumeData for an NSURLSessionDownloadTask?

為{幸葍}努か 提交于 2019-11-29 22:40:22
I have an app that's using background downloads with the new NSURLSession APIs. When a download cancels or fails in such a way that NSURLSessionDownloadTaskResumeData is provided, I store the data blob so that it can be resumed later. A very small amount of the time I am noticing a crash in the wild: Fatal Exception: NSInvalidArgumentException Invalid resume data for background download. Background downloads must use http or https and must download to an accessible file. The error occurs here, where resumeData is the NSData blob and session is an instance of NSURLSession : if (resumeData) {

NSURLSession + server with self signed cert

只愿长相守 提交于 2019-11-29 21:23:33
I have an app that is production along with a development server that has a self signed certificate. I am attempting to test NSURLSession and background downloading but can't seem to get past - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler When I use NSURLConnection I am able to bypass it using: - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *

URLSessionDidFinishEventsForBackgroundURLSession Not Calling- Objective-C

怎甘沉沦 提交于 2019-11-29 19:05:18
问题 NSURLSession Delegate method URLSessionDidFinishEventsForBackgroundURLSession is not Calling ? I already enabled the Background Modes in project capabilities settings. Here is the code AppDelegate.h Method @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (nonatomic, copy) void(^backgroundTransferCompletionHandler)(); @end AppDelegate.m Method -(void)application:(UIApplication *)application

NSURLSession Authentication

烂漫一生 提交于 2019-11-29 18:06:24
I tried to use the swift code found here on the website found here , but the response is html code with the two errors: "You must enter a password!" and "You must enter a User name!" I am new to NSURLSession and tried to change the string for authentication but nothing worked to change the response. Here is my code: let config = NSURLSessionConfiguration.defaultSessionConfiguration() let userPasswordString = "username@gmail.com:password" let userPasswordData = userPasswordString.dataUsingEncoding(NSUTF8StringEncoding) let base64EncodedCredential = userPasswordData!