"photoUpload" might not be a great identifier for a URLSessionConfiguration object

In iOS, there isn't much you can do if your app isn't in the foreground. For certain kinds of apps, that makes programming for iOS more challenging than for Android. One thing you can do is schedule an upload while your app is running, and have the upload happen even if you close down your app. Here's one way that almost makes it work:

  static var urlSession: URLSession = {
    let configuration = URLSessionConfiguration.background(withIdentifier: "photoUpload")
    configuration.sessionSendsLaunchEvents = false
    return URLSession(configuration: configuration)

  }()

  static func startUpload(fileUrl: URL, uploadUrl: URL) {
      var urlRequest = URLRequest(url: uploadUrl)
      urlRequest.setValue("image/jpeg", forHTTPHeaderField: "Content-Type")
      urlRequest.httpMethod = "POST"
      urlSession.uploadTask(with: urlRequest, fromFile: fileUrl).resume()
    }
  }

Have you spotted the bug that cost me half an hour yet? Yes, that's right... the identifier "photoUpload" didn't work.

What?

It turns out that you can use the identifiers "photaUpload", "photoUploads", "asdf" and virtually anything else, but using "photoUpload" causes silent failure. It doesn't even give the standard "this unique identifier isn't actually unique" warning.

If you happen to have a URLSession with a background configuration in your app, why not give the identifier "photoUpload" a try, and see if this happens to you? I'd love to hear from you in the comments.

Comments

Popular posts from this blog

No, programming competitions don't produce bad engineers

Authenticating with Cloud Functions for Firebase from your iOS Swift app

Authenticating phone numbers with Firebase on iOS