urlsession

How to load image asynchronously with Swift using UIImageViewExtension and preventing duplicate images or wrong Images loaded to cells

孤街醉人 提交于 2019-12-21 23:14:46
问题 Im developing an image loading library in Swift 4 something like Kingfisher with some extensions to support loading images from URL into an UIImageView . So then i can use this extension on a UICollection or UITableview cell with an UIImageView like this : let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectioncell", for: indexPath) if let normal = cell as? CollectionViewCell { normal.imagview.loadImage(fromURL:imageURLstrings[indexPath.row]) } Basically this is my

Swift return data from URLSession

风格不统一 提交于 2019-12-20 19:42:49
问题 I cannot return data from my HTTPrequest and I can't get completion handlers to work either. So please assist me in my quest to solve this issue: public static func createRequest(qMes: message, location: String, method: String) -> String{ let requestURL = URL(string: location) var request = URLRequest(url: requestURL!) request.httpMethod = method request.httpBody = qMes.toString().data(using: .utf8) let requestTask = URLSession.shared.dataTask(with: request) { (data: Data?, response:

UI freezes after re-entering Detail-VC during URLSession Background-DownloadTask

巧了我就是萌 提交于 2019-12-13 18:08:27
问题 Using Swift-4.0.3, iOS-11.2, Xcode-9.2, iPhone-6S (or Simulator-10.0) Since about a week I try to figure out what the problem is with the following issue: The UI freezes whenever you re-enter a ViewController that involves a URLSession-Background-DownloadTask. By re-entering, I mean: Going from VC to Detail-VC and back to VC...and then RE-ENTER a second time from VC to the Detail-VC. Please find my example project here on github : https://github.com/korners/Test00008 The sample project uses

Determine when urlsession.shared and Josn parsing are finished

只谈情不闲聊 提交于 2019-12-13 05:27:33
问题 I am downloading and then reading a json file. this json contains a list of files and their address on the server. Everything works fine but I want to get the size of all files to download. but I have some trouble to set up a completionblock that would indicate that everything is finished. here is the code. jsonAnalysis { self.sum = self.sizeArray.reduce(0, +) print(self.sum) } here func jsonAnalysis(completion: @escaping () -> ()) { let urlString = "xxxxxxxxxxxxxxxxxxxxx" let url = URL

IOS: Difference between proposedCredentials and URLCredential for trust

一笑奈何 提交于 2019-12-13 03:16:33
问题 In URLSession:task:didReceiveChallenge:completionHandler:, is there a difference between using .proposedCredential vs URLCredential(trust:) of the server trust object in the challenge protection space? This is to pass back as the second parameter in the completion handler of the delegate method, along with .useCredentials. 回答1: Answer received here: https://forums.developer.apple.com/message/361609 Realistically there’s no difference here. For server trust authentication challenges, the

Swift: URLSession.shared.dataTask says status code 304 = 200?

人走茶凉 提交于 2019-12-11 03:17:18
问题 I'm requesting a JSON-API like the following: var request = URLRequest(url: url) request.httpMethod = "GET" request.addValue(CredentialsProvider.shared.credentials, forHTTPHeaderField: "Authorization") let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in guard let data = data, let _ = response, error == nil else { return } let response1 = response as! HTTPURLResponse print(response1.statusCode) // 200 instead of 304 } As the server does caching (which

Swift URL Session and URL Request not working

帅比萌擦擦* 提交于 2019-12-10 04:13:45
问题 I am getting very similar problems to this post, but I don't fully understand the answer. I've created a completion handler, but it doesn't seem to be working as expected. func updateTeam(teamID: Int) { startConnection {NSArray, Int in //Do things with NSArray } } func startConnection(completion: (NSArray, Int) -> Void) { let url = URL(string: "http://www.example.com/path") var request : URLRequest = URLRequest(url: url!) request.httpMethod = "POST" let postString = "a=\(Int(teamInput.text!)!

Failing to Upload Picture with multipart/form-data to a Server

这一生的挚爱 提交于 2019-12-08 16:11:30
I've read quite a lot of topics here on multipart/form-data . It still doesn't work. I am able to upload a file to my server with URLSession.shared.uploadTask . class MainViewController: UIViewController { @IBOutlet weak var pictureView: UIImageView! @IBAction func postTapped(_ sender: UIButton) { postData() } func postData() { var request = URLRequest(url: URL(string: "http://www.mywebsite.com/upload.php")!) request.httpMethod = "POST" request.timeoutInterval = 30.0 guard let imageData = UIImagePNGRepresentation(pictureView.image!) else { print("oops") return } let uuid = UUID().uuidString

iOS 12 URLSession breaks when HTTP request hits and suddenly app enters in background

若如初见. 提交于 2019-12-08 07:53:30
问题 For all iOS Version < 12, it is working fine. I'm testing(iOS ~> 12.x) in my app using basic URLSession and also tried with Alamofire. Test Steps: 1. Hit Any HTTP/API Call. 2. Tap on home button immediately. On coming after sometime it comes 2018-12-14 13:43:46.968901+0530 NewReader[15364:4847228] Task <519A3F27-90DA-439F-8711-B07EFA62E823>.<1> load failed with error Error Domain=NSPOSIXErrorDomain Code=53 "Software caused connection abort" UserInfo={_NSURLErrorFailingURLSessionTaskErrorKey

How to sync serial queue for URLSession tasks?

ⅰ亾dé卋堺 提交于 2019-12-06 03:42:54
问题 Using XCode-8.2.1, Swift-3.0.2 and iOS-10.2.1, I am trying to call two different URLSession.shared.dataTasks (the first is a simple URL-request and the second is a POST-request). Since my first dataTask delivers a result that is needed in the httpBody of the second dataTask, the two URLSession.shared.dataTasks shall run in series, one after the other! (and also the preparative code shall run consecutively). I tried, so far, using two consecutive serialQueue.sync{} queues. But I had to realize