File path for background upload task on iOS (device and simulator)?

I want to upload a file using uploadTask(with:fromFile:) on iOS (device and simulator).

I tried to save/copy the file to the users documents directory, the temp and caches directory, but for every directory uploadTask terminates the app with 'NSInvalidArgumentException', reason: 'Cannot read file at file:*** -- file:///'

What directory/path do you have to use to get this working?

Or do you have to add something to the Info.plist or at Signing & Capabilities for the app/project?

Accepted Reply

Hello Quinn,

Thanks for the feedback.

I have investigated the URL handling in the code and found a problem converting the URL to a string and back to a URL for using with uploadTask(with:fromFile:).

After fixing the code for converting the URL uploadTask(with:fromFile:) now works as expected.

The provided error message 'NSInvalidArgumentException', reason: 'Cannot read file at file:*** -- file:///' from uploadTask(with:fromFile:) did not lead to look for a problem with the format/content of the URL. To me, the error message had sounded more like an access problem.

Thanks a lot for your support!

Replies

Let’s focus on the device case for the moment, because once you get that working it’s likely that the simulator will just work as well.

In general you can upload a file as long as you have read access to it. A classic example of this is your app’s Documents directory. If you you get this error while trying to upload a file in your Documents directory, you have a bug in your code.

Or do you have to add something to the Info.plist or at Signing & Capabilities for the app/project?

No. This will work out of the box.

The most common cause of this error is a mixup between file paths and file URLs. If you get a file path using a routine like NSSearchPathForDirectoriesInDomains and you need a file URL for that, it’s important to call URL(fileURLWithPath:). If call URL(string:) with a file path, you won’t get the right URL.

IMO the best way to avoid that problem is to always work in ‘URL space’. That has a bunch of other advantages, for example, when dealing with security scopes.

Anyway, if this isn’t the problem please post back with more details about how to build the URL that you pass to the upload task.

Share and Enjoy

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

Hello Quinn,

Thanks for the feedback.

I have investigated the URL handling in the code and found a problem converting the URL to a string and back to a URL for using with uploadTask(with:fromFile:).

After fixing the code for converting the URL uploadTask(with:fromFile:) now works as expected.

The provided error message 'NSInvalidArgumentException', reason: 'Cannot read file at file:*** -- file:///' from uploadTask(with:fromFile:) did not lead to look for a problem with the format/content of the URL. To me, the error message had sounded more like an access problem.

Thanks a lot for your support!