Created
March 28, 2017 06:17
-
-
Save OctoberHammer/76064839c2e3f5dfc3a11877e43f34f6 to your computer and use it in GitHub Desktop.
How To Download Multiple Files Sequentially using NSURLSession downloadTask in Swift
This file contains 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
mport Foundation | |
class DownloadManager { | |
var delegate: HavingWebView? | |
var gotFirstAndEnough = true | |
var finalURL: NSURL?{ | |
didSet{ | |
if finalURL != nil { | |
if let s = self.contentOfURL{ | |
self.delegate?.webView.loadHTMLString(s, baseURL: nil) | |
} | |
} | |
} | |
} | |
var lastRequestBeginning: NSDate? | |
var myLinks = [String](){ | |
didSet{ | |
self.handledLink = self.myLinks.count | |
} | |
} | |
var contentOfURL: String? | |
var handledLink = 0 { | |
didSet{ | |
if handledLink == 0 { | |
self.finalURL = nil | |
print("๐ด๐ถ๐ด๐ถ๐ถ๐ด๐ถ๐ด๐ถ๐ด๐ถ๐ด") | |
} else { | |
if self.finalURL == nil { | |
if let nextURL = NSURL(string: self.myLinks[self.handledLink-1]) { | |
self.loadAsync(nextURL) | |
} | |
} | |
} | |
} | |
} | |
func loadAsync(url: NSURL) { | |
let sessionConfig = NSURLSessionConfiguration.ephemeralSessionConfiguration() | |
let session = NSURLSession(configuration: sessionConfig, delegate: nil, delegateQueue: NSOperationQueue.mainQueue()) | |
let request = NSMutableURLRequest(URL: url, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringCacheData, timeoutInterval: 15.0) | |
request.HTTPMethod = "GET" | |
print("๐") | |
self.lastRequestBeginning = NSDate() | |
print("Requet began: \(self.lastRequestBeginning )") | |
let task = session.dataTaskWithRequest(request, completionHandler: { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in | |
if (error == nil) { | |
if let response = response as? NSHTTPURLResponse { | |
print("\(response)") | |
if response.statusCode == 200 { | |
if let content = String(data: data!, encoding: NSUTF8StringEncoding) { | |
self.contentOfURL = content | |
} | |
self.finalURL = url | |
} | |
} | |
} | |
else { | |
print("Failure: \(error!.localizedDescription)"); | |
} | |
let elapsed = NSDate().timeIntervalSinceDate(self.lastRequestBeginning!) | |
print("trying \(url) takes \(elapsed)") | |
print("๐ Request finished") | |
print("____________________________________________") | |
self.handledLink -= 1 | |
}) | |
task.resume() | |
} | |
} |
This file contains 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 HavingWebView { | |
var webView: UIWebView! {get set} | |
} | |
class ViewController: UIViewController, HavingWebView { | |
@IBOutlet weak var webView: UIWebView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
let dm = DownloadManager() | |
dm.delegate = self | |
dm.myLinks = ["https://medium.com/the-mission/consider-the-present-and-future-value-of-your-decisions-b20fb72f5e#.a12uiiz11", | |
"https://medium.com/@prianka.kariat/ios-10-notifications-with-attachments-and-much-more-169a7405ddaf#.svymi6230", | |
"https://myerotica.com/jingle-bell-fuck-the-twins-5a48782bf5f1#.mjqz821yo", | |
"https://blog.medium.com/39-reasons-we-wont-soon-forget-2016-154ac95683af#.cmb37i58b", | |
"https://backchannel.com/in-2017-your-coworkers-will-live-everywhere-ae14979b5255#.wmi6hxk9p"] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment