Skip to content

Instantly share code, notes, and snippets.

View chriswebb09's full-sized avatar

Christopher Webb chriswebb09

View GitHub Profile
extension ExampleViewController: DownloadDelegate {
func downloadProgressUpdated(for progress: Float) {
DispatchQueue.main.async {
self.progressView.progress += progress
self.downloadProgressLabel.text = String(format: "%.1f%%", progress * 100)
}
}
}
final class Download {
weak var delegate: DownloadDelegate?
// Other properties
var progress: Float = 0.0 {
didSet {
updateProgress()
}
protocol DownloadDelegate: class {
func downloadProgressUpdate(for progress: Float)
}
protocol DownloadDelegate: class {
// Delegate method
}
final class Download {
weak var delegate: DownloadDelegate?
var url: String?
var downloadTask: URLSessionDownloadTask?
var progress: Float = 0.0 {
didSet {
updateProgress()
if progress == 1 {
extension iTunesAPIClient: URLSessionDelegate {
internal func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
if let appDelegate = UIApplication.shared.delegate as? AppDelegate,
let completionHandler = appDelegate.backgroundSessionCompletionHandler {
appDelegate.backgroundSessionCompletionHandler = nil
DispatchQueue.main.async {
completionHandler()
}
}
class AppDelegate: UIResponder, UIApplicationDelegate {
var backgroundSessionCompletionHandler: (() -> Void)?
// AppDelegate functionality.
}
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
// Background completion called here.
}
extension iTunesAPIClient: URLSessionDelegate {
internal func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
// Calls background session completion in AppDelegate
}
internal func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64,totalBytesExpectedToWrite: Int64) {
// Gives you the URLSessionDownloadTask that is being executed
// along with the total file length - totalBytesExpectedToWrite
// and the current amount of data that has received up to this point - totalBytesWritten
}
final class iTunesAPIClient: NSObject {
// Network stuff
}