This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension ExampleViewController: DownloadDelegate { | |
| func downloadProgressUpdated(for progress: Float) { | |
| DispatchQueue.main.async { | |
| self.progressView.progress += progress | |
| self.downloadProgressLabel.text = String(format: "%.1f%%", progress * 100) | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| final class Download { | |
| weak var delegate: DownloadDelegate? | |
| // Other properties | |
| var progress: Float = 0.0 { | |
| didSet { | |
| updateProgress() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protocol DownloadDelegate: class { | |
| func downloadProgressUpdate(for progress: Float) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protocol DownloadDelegate: class { | |
| // Delegate method | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| final class Download { | |
| weak var delegate: DownloadDelegate? | |
| var url: String? | |
| var downloadTask: URLSessionDownloadTask? | |
| var progress: Float = 0.0 { | |
| didSet { | |
| updateProgress() | |
| if progress == 1 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class AppDelegate: UIResponder, UIApplicationDelegate { | |
| var backgroundSessionCompletionHandler: (() -> Void)? | |
| // AppDelegate functionality. | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) { | |
| // Background completion called here. | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| final class iTunesAPIClient: NSObject { | |
| // Network stuff | |
| } |