Post

Replies

Boosts

Views

Activity

URL-Session is not working in Command Line App but in Playground
Hi, I'm new to Xcode and Swift and I want to create a simple app which can get data from a webserver via HTTP Requests. To test this app, I created a Command Line-Project in Xcode. I set up a very simple webserver using Python Flask and searched for code to send URL Requests. I found a code, changed it a bit and tested it in a Swift Playground, and everything worked. So, now I tried to run the same code in a "Command Line Tool" - Project - but nothing happened. After debugging, I found that the "task.resume()"-Function does not execute the code in the "task" anymore. I also exported the project to an executable app, but it still didn't worked. Can someone tell me why one code is working in a Swift Playground but not in a real project? (I just put the code 1:1 into the "main"-file) Here's the code: import Foundation // my Flask-URL guard let myUrl = URL(string: "http://127.0.0.1:5000/get") else {fatalError()} var myReq = URLRequest(url: myUrl) myReq.httpMethod = "GET" let task = URLSession.shared.dataTask(with: myReq) { (data, response, error) in print("Task running...") // This was printed in the playground // but not in the Command Line App if let error = error { print("Error: \(error)") return } if let response = response as? HTTPURLResponse{ print("Status: \(response.statusCode)") } if let data = data, let dataString = String(data: data, encoding: .utf8) { print("Value: \(dataString)") } } task.resume() Please tell me what I did wrong! Tom the axolotl
2
1
1.7k
Aug ’22