ios – Background file upload


I have KMM application. I would like to upload some files on IOS in background.
This multi module app. In module 1 I create files a.json, b.json and in module 2 I create c.json and d.json
All files are created in the same directory.
I call swift method to upload files.
All above is done in Kotlin

In swift I get an array of file paths, I copy files to temp dir and call method:

func addFile(fileURL: URL) {
    guard let endPoint = endPoint else {
        Loge("Endpoint URL is nil.")
        return
    }
    var request = URLRequest(url: URL(string: endPoint)!)
    request.httpMethod = "POST"
    let boundary = "Boundary-\(UUID().uuidString)"
    request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
    let fileData = try? Data(contentsOf: fileURL)
    guard let data = fileData else {
        Loge("Failed to read file data.")
        return
    }
     var body = Data()

      body.append("--\(boundary)\r\n".data(using: .utf8)!)
      body.append("Content-Disposition: form-data; name=\"file\"; filename=\"\(fileURL.lastPathComponent)\"\r\n".data(using: .utf8)!)
      body.append("Content-Type: application/octet-stream\r\n\r\n".data(using: .utf8)!)
      body.append(data)
      body.append("\r\n--\(boundary)--\r\n".data(using: .utf8)!)

    
      request.httpBody = body
    Loge("Start")
    let uploadTask = session!.uploadTask(with: request, from: body)
    uploadTask.taskDescription = fileURL.lastPathComponent
    uploadTask.resume()
    Loge("Stop")
}

For all files created in module 1 it works fine. Files are uploaded and

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)

is called with success for all files.
For files created in module 2 I get between Start and Stop an error:
Thread 22: “Upload tasks from NSData are not supported in background sessions.”
I am sure that all files are in temp dir, all files have 0o644 permission and the same owner. File size is +/- the same

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img