nsurlsession

ambiguous use of selector dataTask(with: completionHandler:)

泄露秘密 提交于 2019-12-21 18:00:30
问题 I'm trying to get a selector of the dataTask(with:completionHandler:) method defined in URLSession which uses URLRequest object like below, but getting error as there are two methods with slightly two different params names (overloaded methods - 1. one uses URLRequest object as param and another uses URL) : let dataTaskSelector = #selector(URLSession.dataTask(with: completionHandler:)) I have tried a different approach like below (mentioned in https://github.com/apple/swift-evolution/blob

Is there a way to use NSURLProtocol in a NSURLSession with custom config?

邮差的信 提交于 2019-12-21 12:59:24
问题 I have a NSURLSession that runs in a background queue. I'm adding my NSURLProtocol subclass to the NSURLsessionConfiguration.protocolClases but the override class func canInitWithRequest(request: NSURLRequest) -> Bool never gets called. This is how I'm adding my NSURLProtocol let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() configuration.protocolClasses!.append(MockNetwork) urlSession = NSURLSession(configuration: configuration, delegate: self, delegateQueue:

How to set cookieAcceptPolicy for ephemeral NSURLSession

自古美人都是妖i 提交于 2019-12-21 09:22:19
问题 I am trying to use ephemeral NSURLSessions to provide separate cookie handling for several tasks within my app. These tasks are not directly bound to a UI. Problem: Whatever I do, the cookieAcceptPolicy of the ephemeral NSHTTPCookieStorage remains NSHTTPCookieAcceptPolicyNever . Here's my code: // use a pure in-memory configuration with its own private cache, cookie, and credential store __block NSURLSessionConfiguration* config = [NSURLSessionConfiguration ephemeralSessionConfiguration]; //

iOS 网络编程:AFNetworking

北城以北 提交于 2019-12-21 08:38:58
1 简介 1.1 概念 AFNetworking 网络框架并不是IOS自带的框架 ,而是第三方的开源框架。它是对NSURLConnection和NSURLSession API的封装,但是目前AFNetworking 3.0已经删除了基于 NSURLConnection API的所有支持,所以本文只记录基于NSURLSession API的相关接口。AFNetworking 框架是基于Object-C语言,若需要使用Swift语言版可以了解 Alamofire 框架。 个人感觉学习AFNetworking 框架非常麻烦,它没有提供guide,所以只能按着GitHub和头文件进行学习。 1.2 第一个程序 1.2.1 环境配置 环境配置非常简单,有多种方式,在GitHub提供了几种方法,这里使用最简单的方式:导入源码。 新建IOS项目后,导入在GitHub网站下载的AFNetworking 源码,如图 2所示。 图 2 1.2.2 源码 其使用如下所示,只需引入AFNetworking.h文件即可使用其API。 1 #include " AFNetworking.h " 2 -( void )DownloadTask 3 { 4 NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration

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

别来无恙 提交于 2019-12-21 06:27:13
问题 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)? 回答1: 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

NSURLSession: background upload and then call a service api

梦想的初衷 提交于 2019-12-21 04:12:28
问题 I was trying to use the new ios7 background transfer api to upload some photos to a server. what it happened now is 1) we upload the bytes to s3 2) call a service api to 'complete' the upload i looked at this doc and it seems background NSURLSession doesn't support 'data' task. does that mean i can't do the step 2 in background after the actual upload is done? 回答1: If you want a simpler solution than repurposing NSURLSessionDownloadTask for your "completed" API call, you can round trip a

HTTP Long Polling in Swift

旧城冷巷雨未停 提交于 2019-12-21 02:41:32
问题 I am trying to implement a long-polling solution in Swift using iOS 8+. While the solution undoubtedly works and leaves the main thread free for UI interactions, the memory usage climbs continuously so I am obviously doing something wrong. The class I have written is as follows: enum LongPollError:ErrorType{ case IncorrectlyFormattedUrl case HttpError } public class LongPollingRequest: NSObject { var GlobalUserInitiatedQueue: dispatch_queue_t { return dispatch_get_global_queue(Int(QOS_CLASS

Uploading Image NSData via POST and NSURLSession

穿精又带淫゛_ 提交于 2019-12-20 21:48:31
问题 I'm trying to upload a single UIImage to a server, and everything seems to be okay except that the image is never uploaded. This is the code I'm using to upload the image in iOS: const NSString *boundaryConstant = @"----------V2ymHFg03ehbqgZCaKO6jy"; const NSString *fileParamConstant = @"photo"; NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil]; NSMutableURLRequest *request = [

Set cookies with NSURLSession

落花浮王杯 提交于 2019-12-20 09:31:16
问题 Hi I am developing one Iphone application In which I want to set cookies after server response and use that for another request. My network request looks like. NSURLSession *session = [NSURLSession sharedSession]; [[session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response; NSLog(@"sttaus code %i", httpResp.statusCode); if (error) { [self.delegate signinWithError:error]; } else { [self

Swift 2 JSON POST request [dictionary vs. String for HTTPBody of NSMutableURLRequest]

≡放荡痞女 提交于 2019-12-20 03:40:39
问题 I have the following swift code that submits a POST request successfully. let request = NSMutableURLRequest(URL: NSURL(string: url)!) let session = NSURLSession.sharedSession() request.HTTPMethod = "POST" request.HTTPBody = "foo=bar&baz=lee".dataUsingEncoding(NSUTF8StringEncoding) let task = session.dataTaskWithRequest(request, completionHandler: completionHandler) Instead of using query parameter like syntax, I'd like to use a dictionary, but when I do the following: let request =