Post

Replies

Boosts

Views

Activity

Upload a zip file in a background URLSession.
I'm trying to upload an existing zip file to a server, but the file itself doesn't seem to be present in the request when I check what the server actually receives. Here's my current implementation: func uploadZip(filePath: URL, authorization: String, callback: @escaping ((Bool) -> Void)) { BGUploadManager.callback = callback let url = URL(string: "\(APINetwork.BASE_URL)")! var request = URLRequest(url: url) request.headers = ["Authorization": "Bearer " + authorization] request.method = HTTPMethod(rawValue: "POST") request.setValue("application/zip", forHTTPHeaderField: "Content-Type") let config = URLSessionConfiguration.background(withIdentifier: "identifier") config.isDiscretionary = false let session = URLSession(configuration: config, delegate: self, delegateQueue: OperationQueue.main) let task = session.uploadTask(with: request, fromFile: filePath) task.resume() } There are implemented handlers for completion and progress and they work properly. The problem is really just the actual file not being sent (or if it is I don't know how to get it). Here's parts of what the server receives: headers: { host: 'localhost:1337', 'content-type': 'application/zip', 'accept-encoding': 'gzip, deflate', connection: 'keep-alive', accept: '*/*', 'user-agent': 'App/37 CFNetwork/1237 Darwin/20.4.0', authorization: 'Bearer sometokenhere', 'content-length': '89634' } body: {} Checking for request.files also shows that it's empty. The Content-Length value is correct. The filePath is the full path and does actually point to the zip file.
1
0
1.5k
Aug ’21
Check if Background URLSession with identifier exists
I have a working background URLSession. I know that upon creating an URLSession with the same session identifier I get a "background URLSession with identifier x already exists" message. I know that I can store the session and call .finishTasksAndInvalidate() on it if needed. My use case is that if the application terminates, and the user relaunches the application before the background task completes, I need to be able to check if a background URLSession with the same identifier exists, and if it does, restitute the application state with the same handlers (so that I can update a UIProgressView for example). I have two questions: How do I check that a background URLSession with a given identifier already exists? Does the AppDelegate completion handler still get called if the application was terminated and relaunched?
5
0
2.2k
Aug ’21