set your Rakefile
app.pods do
pod 'AFNetworking'
...
end
def downloadFile(url, file, filesize)
url = NSURL.URLWithString(url)
request = NSURLRequest.requestWithURL(url)
operation = AFHTTPRequestOperation.alloc.initWithRequest(request)
operation.outputStream = NSOutputStream.outputStreamToFileAtPath(file, append: false)
unless filesize.nil?
SVProgressHUD.showProgress(0, status: "Downloading file")
operation.setDownloadProgressBlock(lambda{|bytesRead, totalBytesRead, totalBytesExpected|
SVProgressHUD.showProgress((((totalBytesRead/filesize.to_f)*100.0).round)/100.0, status: "Downloading file")
})
end
operation.setCompletionBlockWithSuccess(lambda{|request, response|
SVProgressHUD.dismiss unless filesize.nil?
}, failure: lambda{|request, err|
SVProgressHUD.dismiss unless filesize.nil?
@alert = UIAlertView.alloc.initWithTitle('Error',
message: 'Error when downloading data.', delegate: nil, cancelButtonTitle: 'OK',
otherButtonTitles: nil)
@alert.show
})
operation.start
end
when downloading multiple files, use NSOperationQueue
# change donwloadFile def downloadFile.. ... operation # was operation.start end
@queue = NSOperationQueue.alloc.init @queue.name = "FileDownload" @queue.maxConcurrentOperationCount = 1 # number of concurrent downloads @queue.addOperation(downloadFile(url, file, filesize))