nsurlsession

Is there any way to attach a NSDictionary of parameters to an NSURLRequest instead of manually making a string?

守給你的承諾、 提交于 2019-11-28 07:02:56
问题 AFNetworking allows you to add an NSDictionary of parameters to a request, and it will append them to the request. So if I wanted to do a GET request with ?q=8&home=8888 I'd just making an NSDictionary like @{@"q": @"8", @"home": @"8888"} very simply. Is there a way to do this with NSURLSession / NSURLConnection / NSURLRequest ? I know I can use NSJSONSerialization for appending JSON data, but what if I just want them as GET parameters in the URL? Should I just add a category? 回答1: You can do

Background upload multiple images using single NSURLSession uploadTaskWithRequest

僤鯓⒐⒋嵵緔 提交于 2019-11-28 07:02:04
I want to upload multiple images in background using a single uploadTaskWithRequest method. While trying the following code returns Upload tasks from NSData are not supported in background sessions...please how to achieve this func createRequest (param : NSDictionary ,imagearray :NSMutableArray, strURL : String) -> NSURLRequest { let boundary = generateBoundaryString() let url = NSURL(string: strURL) let request = NSMutableURLRequest(URL: url!) request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") request.HTTPMethod = "POST" request.HTTPBody =

iOS/Cocoa - NSURLSession - Handling Basic HTTPS Authorization

筅森魡賤 提交于 2019-11-28 06:31:54
[edited to provide more information] (I'm not using AFNetworking for this project. I may do so in future, but wish to resolve this problem/misunderstanding first.) SERVER SETUP I cannot provide the real service here, but it is a simple, reliable service that returns XML according to a URL such as: https://username:password@example.com/webservice I want to connect to the URL over HTTPS using GET, and determine any authentication failures (http status code 401). I have confirmed that the web service is available, and that I can successfully (http status code 200) grab XML from the url using a

GET request with parameters

允我心安 提交于 2019-11-28 06:14:50
问题 Which way is recomed in swift 3 for GET with parameters ? Example : https://my-side.com/data?token=AS7F87SAD84889AD/ Thanks in advance ! 回答1: Example how to use URLQueryItem for the requests. func getRequest(params: [String:String]) { let urlComp = NSURLComponents(string: "https://my-side.com/data")! var items = [URLQueryItem]() for (key,value) in params { items.append(URLQueryItem(name: key, value: value)) } items = items.filter{!$0.name.isEmpty} if !items.isEmpty { urlComp.queryItems =

iOS 9 ATS SSL error with supporting server

橙三吉。 提交于 2019-11-28 04:24:32
I installed Xcode 7 and tried running my app under iOS 9. I'm getting the infamous error: Connection failed! Error - -1200 An SSL error has occurred and a secure connection to the server cannot be made. The thing is my server DOES support TLSv1.2 and I'm using NSURLSession . What could be the problem then? YogevSitton Apple has released the full requirements list for the App Transport Security . Turned out that we were working with TLS v1.2 but were missing some of the other requirements. Here's the full check list: TLS requires at least version 1.2. Connection ciphers are limited to those

How to cache using NSURLSession and NSURLCache. Not working

巧了我就是萌 提交于 2019-11-28 04:09:42
I have a test app setup and it successfully downloads content from the network even if the user switches apps while a download is in progress. Great, now I have background downloads in place. Now I want to add caching. There is no point to me downloading images more than once, b/c of system design, given an image URL I can tell you the content behind that URL will never change. So, now I want to cache the results of my download using apple's built in in-memory/on-disk cache that I've read so much about (as opposed to me saving the file manually in NSCachesDirectory and then checking there

session.dataTaskWithURL completionHandler never called

风格不统一 提交于 2019-11-28 03:56:19
问题 I have the following code : let urlPath:String = apiURL + apiVersion + url + "?api_key=" + apiKey let url = NSURL(string: urlPath) let session = NSURLSession.sharedSession() println(url!) let task = session.dataTaskWithURL(url!, completionHandler: {(data, reponse, error) in println("Task completed") // rest of the function... }) The completionHandler function is never called. I tried calling the URL in my browser, it works fine. I tried with another URL, it still doesn't work. I checked that

Function does not wait until the data is downloaded

戏子无情 提交于 2019-11-28 01:51:37
I have the following function which downloads an image from server; func getImageFromServerById(imageId: String) -> UIImage? { let url:String = "https://dummyUrl.com/\(imageId).jpg" var resultInNSDataformat: NSData! let task = NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: url)!) {(data, response, error) in if (error == nil){ resultInNSDataformat = data } } task.resume() return UIImage(data: resultInNSDataformat) } The function does not wait for the download task to be completed before returning the image. Therefore my app always crashes. Any ideas for how to wait for the download?

POST data to a PHP method from Swift

只愿长相守 提交于 2019-11-28 00:47:58
问题 I'm trying to post some info to my PHP file from Swift. My php file is executed, but the posted variables just don't get through to the php file. What am I doing wrong? Swift code: @IBAction func buttonPress(sender: AnyObject) { let request = NSMutableURLRequest(URL: NSURL(string: "http://www.domain.com/php_swift_test/insert.php")!) request.HTTPMethod = "POST" let postString = "a=test&b=bla" request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding) let task = NSURLSession

How do I get the NSURLResponse before the downloadTaskWithURL finishes?

戏子无情 提交于 2019-11-27 22:42:53
问题 This is my code for download : let url = NSURL(string:"http://www.zastavki.com/pictures/originals/2013/Photoshop_Image_of_the_horse_053857_.jpg")! let documentsDirectoryURL = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first as! NSURL NSURLSession.sharedSession().downloadTaskWithURL(url, completionHandler: { (location, response, error) -> Void in if let error = error { println(error.description) } else { println("Finished downloading \"\(response