nsurlsession

NSURLSession completion block not called

本秂侑毒 提交于 2019-12-04 00:29:45
var session = NSURLSession.sharedSession() session.dataTaskWithRequest(urlRequest, completionHandler: {(data: NSData!, response: NSURLResponse!, error: NSError!) in println(data) println(response) println(error) }) So I am making this request, and the completion block is never called. What's wrong? Also I tried a synchronous and asynchronous form of the same request with NSURLConnection and it worked perfectly. EDIT: I tried assigning a dataTask variable to the session.dataTaskWithRequest and displayed it right after. It says this <__NSCFLocalDataTask: 0xb41bda0> { suspended } Suspended? Why?

unable to upload file using NSURLSession multi-part form data in iOS

被刻印的时光 ゝ 提交于 2019-12-04 00:04:11
I am trying to upload a video / image file using - (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL; method using multi-part form data. But somehow i am not able to upload the file and i am getting " stream ended unexpectedly " error. Requirements Upload a video / image file to server App should support background uploads (Continue the upload process even after app goes into background) Server expects the data to be sent using multi-part form data. Methods / API's used to achieve this NSURLSession background session API (Complete code listed

How can we upload multipart form data with nested JSON parameters in Swift?

一世执手 提交于 2019-12-03 21:33:59
I need to upload an image to the server endpoint where the structure has to be as following: { "image": { "file": imageData }, "access_token": access_token } How can I send such a request using NSURLSession (or maybe even Alamofire or AFNetworking)? Rob You cannot just include binary image data in a JSON request. JSON requires text representation, so if you do this, you must convert it to string (e.g. base64 encoding), use that in the JSON, and then the server code would presumably convert the base64 string back to binary data before attempting to use it. But if you were base64 encoding of the

NSURLSession delegation: How to implement my custom SessionDelegate class accordingly?

霸气de小男生 提交于 2019-12-03 20:45:06
Got a singleton class, so called RequestManager, which shall handle requests made by different modules and background tasks of my application. @interface RequestFactory : NSObject - (void)requestDataWith:(NSString *)token id:(NSString *)id sender:(id<RequestFactoryDelegate>)sender; ... @end Then I got another class, so called SessionDelegate, which shall handle all the callbacks during the request. @interface SessionDelegate : NSObject <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate> @property (weak, nonatomic) id <RequestFactoryDelegate> delegate; @end My idea is to

NSURLSession cancel Task

回眸只為那壹抹淺笑 提交于 2019-12-03 20:19:44
问题 I create new NSURLSession with following configs if (!self.session) { NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfiguration:[self uniquieIdentifier]]; config.discretionary = NO; self.session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]]; } and on after pressing a button I am trying to stop all current download tasks. [[[self session] delegateQueue] setSuspended:YES]; [[self session]

NSURLSession and stream upload in background

限于喜欢 提交于 2019-12-03 20:01:19
问题 I have some problems with using NSURLSession to upload photos from Asset Library to the server. At first NSURLSession doesn't support streaming upload. I got an exception when trying to using that: @property (nonatomic, strong) NSURLSession *uploadSession; ... _uploadSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration backgroundSessionConfiguration:kUploadBackgroundURLSessionIdentifier] delegate:self delegateQueue:nil]; ... NSURLSessionUploadTask *task = [self

NSURLSessionDownloadTask move temporary file

混江龙づ霸主 提交于 2019-12-03 18:13:52
问题 I'm trying to reach the file which I was downloading earlier by NSURLSession . It seems I can't read the location of the file, even though I'm doing it before delegate method ends (as the file is temporary). Still, I'm getting nil when trying to access the data under location returned from NSURLSession delegate and error 257. The code goes as following: - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location

Networking pattern based on NSURLSession

℡╲_俬逩灬. 提交于 2019-12-03 14:38:41
I've been traditionally using a pattern where NSOperation subclasses create and manage their own NSURLConnection. The NSOperation subclass is instantiated by the view controller and will do its work without bothering the controller until it has finished. When it finishes retrieving data, it executes the completion block supplied by the view controller. ViewController instantiates NSOperation subclass (which encapsulates URL, parameters etc) NSOperation subclass instanciates NSURLConnection (which performs synchronous request and retrieves data) NSURLConnection dumps data to NSOperation

Maintain Session information by handling cookies in iOS

牧云@^-^@ 提交于 2019-12-03 14:00:31
问题 I am new in iOS development. I am using NSURLSession to manage session information. below is the sample code I use to call any server API, NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { }]; My flow of application is, If not logged in -> Login (call login api) Else Go to home screen and call other APIs. My problem here is, once the application is removed from memory, the session

What is difference between NSURLSessionDataTask vs NSURLSessionDownloadTask

本小妞迷上赌 提交于 2019-12-03 10:48:00
问题 In latest apple introduce new NSURLSession in replace of NSURLConnection , so in there are different task , so what is the difference between NSURLSessionDataTask , NSURLSessionDownloadTask ? and in which scenario use NSURLSessionTask and where NSURLSessionDownloadTask ? 回答1: NSURLSessionDataTask : Data tasks exchange data using NSData. NSURLSessionDataTask is not supported in Background Sessions. Data tasks send and receive data using NSData objects. Data tasks are intended for short, often