Posts

Showing posts from September, 2017

"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 ()      }