Why does file download work on simulator but not on device?

Hi Apple developers,

I am new to IOS development and have recently added to my app the ability to download a file from a web API I created to be used in the app. The strange thing is this works fine in the simulator but when I plug in my IOS device I get these two errors:

nw_endpoint_handler_set_adaptive_read_handler [C2 192.168.1.67:5000 ready channel-flow (satisfied (Path is satisfied), viable, interface: en0, ipv4, dns)] unregister notification for read_timeout failed

and

nw_endpoint_handler_set_adaptive_write_handler [C2 192.168.1.67:5000 ready channel-flow (satisfied (Path is satisfied), viable, interface: en0, ipv4, dns)] unregister notification for write_timeout failed

Other endpoints from the web API that do not involve file downloads still work on the device.

The file type is a usdz model

Here is the code used to download and store the file:

class ModelFetcher: NSObject{
  var modelUrl: URL?
   
  func generateModel() {
    guard let url = URL(string: "http://192.168.1.67:5000/model.usdz") else {return}
    let urlSession = URLSession(configuration: .default, delegate: self, delegateQueue: OperationQueue())
    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    let downloadTask = urlSession.downloadTask(with: request)
    downloadTask.resume()
  }
}

extension ModelFetcher: URLSessionDownloadDelegate {
  func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
    print("File Downloaded Location- ", location)
     
    let docsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let destinationPath = docsPath.appendingPathComponent("model.usdz")
     
    try? FileManager.default.removeItem(at: destinationPath)
     
    do {
      try FileManager.default.copyItem(at: location, to: destinationPath)
      self.modelUrl = destinationPath
      print("File moved to: \(modelUrl!)")
    } catch let error {
      print("Copy Error: \(error.localizedDescription)")
    }
  }
}

Perhaps I have missed some configuration setting needed to allow file downloads on a device? Because, again, it works on the simulator but not on my own device, and, again, the other plain JSON endpoints still work on the device.

Any help would be much appreciated.

Thanks

Louis

Answered by DTS Engineer in 687646022

If you implement the urlSession(_:task:didCompleteWithError:) delegate method, what do you see?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

If you implement the urlSession(_:task:didCompleteWithError:) delegate method, what do you see?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

However, I still get the same result and when the download is attempted Xcode shows a thread breakpoint in the first line of the urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) function.

I’m confused. If the did-finish-downloading-to delegate method is called, that means the download was successful. What exactly is the problem here?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Though I would be interested in what the 2 messages output to the console in Xcode mean

I’d be inclined to label them as log noise.

why they only show up when using a physical device rather than a simulator

There are lots of differences between the simulator and a real device, especially as you descend lower into the technology stack. This specific issue relates to the network subsystem underlying NSURLSession, the same subsystem that backs Network framework. It has two implementations, one that works in user space and one that talks to the kernel via BSD Sockets. A real device uses the former [1] whereas the simulator uses the latter [2]. I expect that this log message is being generated by the user space implementation, and that’s why it doesn’t show up on the simulator.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] For more background on this, see WWDC 2018 Session 715 Introducing Network.framework: A modern alternative to Sockets.

[2] User-space networking was originally not enabled on macOS because of the requirement to support NKEs. Now that NKEs are a thing of the past, I wouldn’t be surprised if that changed.

Why does file download work on simulator but not on device?
 
 
Q