After watching the WWDC session on networking and hearing general advice to avoid using Reachability for pre-flight checks, I was reminded of a time I do implement a pre-flight check. I looked at removing this from my app, but I ran into a snag. I was hoping to get some advice on the following scenario:
I have an app that lets users download videos to their device. If they are on cellular, I present a warning that they will be downloading XX MB on cellular, and if they accept, I toggle an option in UserDefaults to allowCellularDownloads.
The relevant code looks like this:
// if we're on cellular and cellular downloads are turned off, then the download
// will start in a pending state and continue when wifi is reached. We don't get
// a notification or callback for this, so the UI will look like the UI is stuck
// instead we'll check now and update the messaging to better communicate this
let onCellular = self.reachability?.status == .reachableViaWWANThen we configure an alert view with an action that reads the user defaults setting:
alert.addAction(UIAlertAction(title: "Download Over Cellular", style: .default, handler: { action in
if Settings.shared.allowDownloadingOverCellular {
self.startDownload()
} else {
let confirm = UIAlertController(title: "Allow Cellular Downloads?",
message: "This can be adjusted later in settings", preferredStyle: .alert)
confirm.addAction(UIAlertAction(title: "Enable", style.default, handler: { ... })
confirm.addAction(UIAlertAction(title: "Download When on Wi-Fi", style.default, handler: { _ in
// will just pause silently until we are on wifi
self.startDownload()
})
// ...
}The downloader sets the allowsCellularAccess on the session used for downloads based on their setting:
let config = URLSessionConfiguration.background(withIdentifier: sessionIdentifier)
config.allowsCellularAccess = Settings.shared.allowDownloadingOverCellularIn this scenario, how do I properly communicate this to the user without using reachability?