nsurlsession

Swift 2 iOS 9 Do Catch Try crashing with unexpected nil found

◇◆丶佛笑我妖孽 提交于 2019-11-26 18:27:21
问题 I'm trying to become familiar with the new do catch statements with swift 2 and iOS 9 My problem is that when an error occurs with NSURLSession, the data parameter returns nil, and error returns something. In iOS 8 this was expected functionality and we simply used if statements to find out whether or not Data was nil However with do catch , there is the new try keyword which I thought was meant to see if something works, if it doesn't then default to whatever code is written in catch However

Showing the file download progress with NSURLSessionDataTask

ぐ巨炮叔叔 提交于 2019-11-26 18:11:38
问题 I want to display file download progress (how many bytes are received) of particular file. It works fine with the NSURLSessionDownloadTask .My question is I want to achieve the same with the NSURLSessionDataTask. Here is the code which receives file into NSData and writes to document folder: NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject

URLSession.datatask with request block not called in background

冷暖自知 提交于 2019-11-26 17:46:53
问题 URLSession data task block is not calling when the app is in background and it stuck at dataTask with request. When I open the app the block gets called. By the way I'm using https request. This is my code: let request = NSMutableURLRequest(url: URL(string: url as String)!, cachePolicy: .reloadIgnoringCacheData, timeoutInterval:20) request.httpMethod = method as String request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") let session = URLSession.shared let

What is the biggest difference between NSURLConnection and NSURLSession

不羁的心 提交于 2019-11-26 15:48:26
问题 NSURLSession is new network SDK than NSURLConnection from Apple. 3rd old choice is CFNetwork. Question here is to figure out the biggest difference between them to understand why Apple is evolving like these. Thanks 回答1: The entire model is different. NSURLSession is designed around the assumption that you'll have a lot of requests that need similar configuration (standard sets of headers, etc.), and makes life much easier if you do. NSURLSession also provides support for background downloads

Send POST request using NSURLSession

孤人 提交于 2019-11-26 12:34:50
Update: Solution found. You can read it at the end of the post. I'm trying to perform a POST request to a remote REST API using NSURLSession . The idea is to make a request with two parameters: deviceId and textContent . The problem is that those parameters are not recognized by the server. The server part works correctly because I've sent a POST using POSTMAN for Google Chrome and it worked perfectly. This is the code I'm using right now: NSString *deviceID = [[NSUserDefaults standardUserDefaults] objectForKey:@"deviceID"]; NSString *textContent = @"New note"; NSString *noteDataString =

How to get server response data in NSURLSession without completion block

早过忘川 提交于 2019-11-26 11:59:25
问题 I am using NSURLSession for background image uploading. And according to uploaded image my server gives me response and I do change in my app accordingly. But I can\'t get my server response when my app uploading image in background because there is no completion block. Is there way to get response without using completion block in NSURLUploadTask ? Here is my code : self.uploadTask = [self.session uploadTaskWithRequest:request fromData:body completionHandler:^(NSData *data, NSURLResponse

How to use special character in NSURL?

回眸只為那壹抹淺笑 提交于 2019-11-26 11:54:17
My application is using an NSURL like this: var url = NSURL(string: "http://www.geonames.org/search.html?q=Aïn+Béïda+Algeria&country=") When I tried to make a task for getting data from this NSURL like this: let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in if error == nil { var urlContent = NSString(data: data, encoding: NSUTF8StringEncoding) println("urlContent \(urlContent!)") } else { println("error mode") } but I got error when trying to got data from that address, although when I using

Resume NSUrlSession on iOS10

半城伤御伤魂 提交于 2019-11-26 11:49:50
问题 iOS 10 is going to be released soon so it worth to test applications for compatibility with it. During such test we\'ve discovered that our app can\'t resume background downloads on iOS10. Code that worked well on previous versions does not work on new one, both on an emulator and on a device. Instead of reducing our code to minimal working test case I\'ve searched internet for NSUrlSession tutorials and tested them. Behaviour is the same: resuming works on previos versions of iOS but breaks

Swift: How do I return a value within an asynchronous urlsession function?

两盒软妹~` 提交于 2019-11-26 10:36:05
As you can see, I'm receiving a JSON file, parsing it using SwiftyJSON, and trying to return totalTime, but it won't let me. How do I do this? func googleDuration(origin: String, destination: String) -> Int{ // do calculations origin and destiantion with google distance matrix api let originFix = origin.stringByReplacingOccurrencesOfString(" ", withString: "+", options: NSStringCompareOptions.LiteralSearch, range: nil); let destinationFix = destination.stringByReplacingOccurrencesOfString(" ", withString: "+", options: NSStringCompareOptions.LiteralSearch, range: nil); let urlAsString = "https

NSURLSession concurrent requests with Alamofire

和自甴很熟 提交于 2019-11-26 08:51:48
问题 I\'m experiencing some strange behaviour with my test app. I\'ve about 50 simultaneous GET requests that I send to the same server. The server is an embedded server on a small piece of hardware with very limited resources. In order to optimize the performance for each single request, I configure one instance of Alamofire.Manager as follows: let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() configuration.HTTPMaximumConnectionsPerHost = 2 configuration