nsurlsession

Swift 4 - Get an Upload Image Progress using URLSession

我们两清 提交于 2019-11-29 18:03:25
I have this kind of code below func upload(){ let img = UIImage(named: "1") let imgData = UIImageJPEGRepresentation(img!, 1) let data = imgData?.base64EncodedString() var request = URLRequest(url: URL(string: url)!) request.httpMethod = "POST" request.setValue("\(auth)", forHTTPHeaderField: "Authorization") request.setValue("application/xml", forHTTPHeaderField: "Content-Type") var postString = "<uploadrequest>" postString += "<folderid>123</folderid>" postString += "<folder_name>images</folder_name>" postString += "<image_byte>\(data!)</image_byte>" postString += "</uploadrequest>" request

NSURLSessionDownloadTask move temporary file

我怕爱的太早我们不能终老 提交于 2019-11-29 16:16:06
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 { NSError *movingError = nil; NSData *fileData = [NSData dataWithContentsOfFile:location.path options:0

Manage the number of active tasks in a background NSURLSession

落花浮王杯 提交于 2019-11-29 15:57:13
It seems that when you enqueue too many tasks (say, hundreds) on a background NSURLSession , it doesn't work well. How can you keep the number of enqueued tasks at a small fixed number, say 10? In the class that is responsible for enqueueing tasks, have a ivar to track the active tasks, e.g. // In MySessionWrapper.m @interface MySessionWrapper () { NSMutableSet *activeTaskIds; } @end When you enqueue a task, you add its ID to that set: [activeTaskIds addObject:@([task taskIdentifier])] When you get a didComplete callback, you remove the ID, and if the number of active tasks falls below your

Expand a short URL in Swift [closed]

我的未来我决定 提交于 2019-11-29 15:46:03
问题 Given a short URL https://itun.es/us/JB7h_, How do you expand it into the full URL? 回答1: Extension extension NSURL { func expandURLWithCompletionHandler(completionHandler: (NSURL?) -> Void) { let dataTask = NSURLSession.sharedSession().dataTaskWithURL(self, completionHandler: { _, response, _ in if let expandedURL = response?.URL { completionHandler(expandedURL) } }) dataTask.resume() } } Example let shortURL = NSURL(string: "https://itun.es/us/JB7h_") shortURL?.expandURLWithCompletionHandler

How to use URLSessionStreamTask with URLSession for chunked-encoding transfer

此生再无相见时 提交于 2019-11-29 15:29:56
问题 I am trying to connect to the Twitter streaming API endpoint. It looks like URLSession supports streaming via URLSessionStreamTask , however I can't figure out how to use the API. I have not been able to find any sample code either. I tried testing the following, but there is no network traffic recorded: let session = URLSession(configuration: .default, delegate: self, delegateQueue: nil) let stream = session.streamTask(withHostName: "https://stream.twitter.com/1.1/statuses/sample.json", port

Does NSUrlSession continue file transfer if the app is killed from task manager?

人走茶凉 提交于 2019-11-29 14:46:32
问题 I have tried various samples from the web (the last one being this one) in order to get a better understanding of NSUrlSession . What I was hoping to see: file downloads will continue even if the app that triggered them gets killed (for instance by the user through the task manager). However this does not seem to happen. Is this a configuration issue or does background file transfer not work if the app gets terminated? I thought the whole idea was that iOS will restart the app. 回答1: If the

WKURLSessionRefreshBackgroundTask isn't called when attempting to do background refreshes in watchOS

你。 提交于 2019-11-29 14:39:39
问题 I'm working on a complication to get scheduled data from a web service. Every 20-30 minutes (or manually), I am scheduling a WKRefreshBackgroundTask to do this. As suggested by Apple, I want the OS to handle the fetching of this data via a background NSURLSession . This is the function I use to download the data I need: func scheduleURLSession() { print("\nScheduling URL Session...") let backgroundSessionConfig:URLSessionConfiguration = URLSessionConfiguration.background(withIdentifier:

Replacing NSURLConnection with NSURLSession

拜拜、爱过 提交于 2019-11-29 13:48:03
问题 I have been starting a design for NetworkCommunication . I had a design where NSOperation subclasses create and manage their own NSURLConnection . The NSOperation subclass is instantiated by a NetworkManger class which adds it to the NSOperationQueue . Once the request is finished the delegate(ex: ViewController will be called). This is the flow: The Network Manger instantiates NSOperation subclass (which encapsulates URL, parameters etc) and adds it to a NSOperationQueue it maintains.

session.dataTaskWithURL completionHandler never called

China☆狼群 提交于 2019-11-29 10:37:49
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 my ios simulator could connect to the Internet, it does. I don't know why the function is not called

POST data to a PHP method from Swift

荒凉一梦 提交于 2019-11-29 07:12:53
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.sharedSession().dataTaskWithRequest(request) { data, response, error in if error != nil { print("error=\(error)