nsurlsession

iOS NSURLSession, how to retry in didCompleteWithError

我怕爱的太早我们不能终老 提交于 2019-12-24 00:55:37
问题 I want to try a post call on my server till it success, I would like to try it each 30 seconds. So I am using NSURLSession for my call: NSURLSessionDownloadTask *task = [self.session downloadTaskWithRequest:request]; task.taskDescription = [NSString stringWithFormat:@"Downloading file %@", [path lastPathComponent]]; [task resume]; Then if an error occur (no network for example) I have an error in: -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:

Xcode8 beta 6 - URLSession with completionHandler argument not working

百般思念 提交于 2019-12-24 00:48:46
问题 I can't seem to use this method in my code at all after changing from beta 5 til beta 6. open func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Swift.Void) -> URLSessionDataTask My code: let task = self.createSession().dataTask(with: request, completionHandler: { (data, response, error) in self.handleTaskResult(data: data, response: response, error: error, completionHandler: completionHandlerIncoming) }) I get that "Cannot invoke 'dataTask'

HTTP PUT method with NSURLSession

余生颓废 提交于 2019-12-23 07:26:48
问题 I'm struggling with NSURLSession class introduced in iOS 7 SDK and I'm stuck at a point, where I need to call my REST API with PUT method. I've already implemented GET and POST methods, which are working fine using this: NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request addValue:@"application/json" forHTTPHeaderField:@

Error Domain=NSURLErrorDomain Code=-1004 instead of -1009 in Swift service calls

↘锁芯ラ 提交于 2019-12-23 02:38:12
问题 Generally, while performing GET or POST calls in iOS using dataTaskWithRequest or sendAsynchronousRequest we use to face network related errors with error codes like, NSURLErrorNotConnectedToInternet = -1009 NSURLErrorCannotConnectToHost = -1004 NSURLErrorTimedOut = -1001 In my case i'm disconnecting the internet and performing service calls. So, the expected error code is "NSURLErrorNotConnectedToInternet = -1009" . But, its throwing "NSURLErrorCannotConnectToHost = -1004" like below, Error

Return object for a method inside completion block

夙愿已清 提交于 2019-12-23 02:34:45
问题 I want to make a method with URL parameter that returns the response of calling that URL. How can I return the data obtained inside a completion block for a method? class func MakeGetRequest(urlString: String) -> (data: NSData, error: NSError) { let url = NSURL(string: urlString) var dataResponse: NSData var err: NSError let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in //How can I return the data obtained here.... }) task

NSURLSession Upload File To Server Swift

偶尔善良 提交于 2019-12-23 01:37:17
问题 I'm creating a Internet Speed testing app and can't seem to get it right to upload the zip file to the ftp server. It is an open server. I want to be able to upload the file and show the progress. The func URLSession(session: NSURLSession, task: NSURLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) isn't working. import UIKit import Foundation class Upload: UIViewController, NSURLSessionTaskDelegate, NSURLSessionDataDelegate,

Sending simple POST in Swift, getting “Empty Post” response

我的梦境 提交于 2019-12-23 01:24:31
问题 I have registration page in my iOS app that I'm trying to write in Swift. The first thing I'm testing out is sending a POST with the email address, this is the way I'm doing so: var bodyData = ("userEmail=%@\" \" &userPassword=%@\" \"&userDevice=%@\" \"", emailAddress.text, password.text, deviceModel) let dataToSend = (bodyData as NSString).dataUsingEncoding(NSUTF8StringEncoding) request.HTTPMethod = "POST" request.HTTPBody = dataToSend let task = NSURLSession.sharedSession()

NSURLSession make sure Upload worked

半城伤御伤魂 提交于 2019-12-23 01:03:30
问题 What's the best approach to make sure that an upload works? I need to upload images, to a server and make sure, that the upload has worked. If, for whatever reason, the upload did not work, I'll have to retry later. Currently, I'm using NSUrlSession to upload the image: - (void)didCreateSignature:(UIImage *)image { BLog(); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString

Execute Code on Main Thread from Async F#

廉价感情. 提交于 2019-12-22 12:18:41
问题 I am implementing the following Swift method in F#: func downloadCachedImage(url : URL) { if let cachedImage = imageCache.object(forKey: url.absoluteString as AnyObject) { self.image = cachedImage as! UIImage } URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in if (error != nil) { print(error) return } DispatchQueue.main.async { if let downloadedim = UIImage(data: data!) { imageCache.setObject(downloadedim, forKey: url.absoluteString as AnyObject) self.image

Background transfer download task failed when app was closed

我的梦境 提交于 2019-12-22 11:20:02
问题 I have created background nsurlsession to perform download task. It worked well when the app was in background. However, download task seems to be canceled and failed when I closed the app (double click "Home" button and swipe up), and it made me to download from the beginning again when I relaunched the app. According to Apple document, background transfer works even the app is no longer running. Am I doing anything wrong? 回答1: From the NSURLSessionConfiguration Class Reference: If an iOS