My goal is to download an array of URLs (for example, 25 URLs) as strings, and report when the URLs have all finished downloading.
At the moment, I am able to successfully download from 1 webpage, as configured below:
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://www.apple.com")
self.downloadWebpage(url: url!)
}
func getDataFromUrl(url: URL, completion: #escaping (_ data: Data?, _ response: URLResponse?, _ error: Error?) -> Void) {
URLSession.shared.dataTask(with: url) { (data, response, error) in
completion(data, response, error)
if error != nil {
print (error.debugDescription)
}
}.resume()
}
func downloadWebpage(url: URL) {
getDataFromUrl(url: url) { (data, response, error) in
guard let data = data, error == nil else { return }
print(response?.suggestedFilename ?? url.lastPathComponent)
print("Download Finished")
print (data)
DispatchQueue.main.async() { () -> Void in
if let returnData = String(data: data, encoding: .utf8) {
print (returnData)
}
}
}
}
While I realize I could easily package my URLs inside an array, my difficulty is about getting the process to report that when all of the URLs from the array are downloaded (or, have error'ed out). Any assistance is appreciated.
Quick way
NSNotifications
You set up a notification to fire when your downloads are done, then listen for that notification elsewhere in the application.
This article explains how;
https://blog.bobthedeveloper.io/pass-data-with-nsnotification-in-swift-3-73743723c84b
Proper way
Competition handlers
https://thatthinginswift.com/completion-handlers/
Related
I am obviously missing something very fundamental/naïve/etc., but for the life of me I cannot figure out how to make simple GET requests.
I'm trying to make an HTTP GET request with Swift 5. I've looked at these posts/articles: one, two, but I can't get print() statements to show anything. When I use breakpoints to debug, the entire section within the URLSession.shared.dataTask section is skipped.
I am looking at the following code (from the first link, above):
func HTTP_Request() {
let url = URL(string: "http://www.stackoverflow.com")!
let task = URLSession.shared.dataTask(with: url) {(data: Data?, response: URLResponse?, error: Error?) in
guard let data = data else { return }
print(String(data: data, encoding: .utf8)!)
}
task.resume()
}
HTTP_Request()
I am running this in a MacOS Command Line Project created through XCode.
I would greatly appreciate any help I can get on this, thank you.
Right now, if there is an error, you are going to silently fail. So add some error logging, e.g.,
func httpRequest() {
let url = URL(string: "https://www.stackoverflow.com")! // note, https, not http
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard
error == nil,
let data = data,
let string = String(data: data, encoding: .utf8)
else {
print(error ?? "Unknown error")
return
}
print(string)
}
task.resume()
}
That should at least give you some indication of the problem.
A few other considerations:
If command line app, you have to recognize that the app may quit before this asynchronous network request finishes. One would generally start up a RunLoop, looping with run(mode:before:) until the network request finishes, as advised in the run documentation.
For example, you might give that routine a completion handler that will be called on the main thread when it is done. Then you can use that:
func httpRequest(completion: #escaping () -> Void) {
let url = URL(string: "https://www.stackoverflow.com")! // note, https, not http
let task = URLSession.shared.dataTask(with: url) { data, response, error in
defer {
DispatchQueue.main.async {
completion()
}
}
guard
error == nil,
let data = data,
let string = String(data: data, encoding: .utf8)
else {
print(error ?? "Unknown error")
return
}
print(string)
}
task.resume()
}
var finished = false
httpRequest {
finished = true
}
while !finished {
RunLoop.current.run(mode: .default, before: .distantFuture)
}
In standard macOS apps, you have to enable outgoing (client) connections in the “App Sandbox” capabilities.
If playground, you have to set needsIndefiniteExecution.
By default, macOS and iOS apps disable http requests unless you enable "Allow Arbitrary Loads” in your Info.plist. That is not applicable to command line apps, but you should be aware of that should you try to do this in standard macOS/iOS apps.
In this case, you should just use https and avoid that consideration altogether.
Make sure the response get print before exiting the process, you could try to append
RunLoop.main.run()
or
sleep(UINT32_MAX)
in the end to make sure the main thread won't exit. If you want to print the response and exit the process immediately, suggest using DispatchSemaphore:
let semphare = DispatchSemaphore(value: 0)
func HTTP_Request() {
let url = URL(string: "http://www.stackoverflow.com")!
let task = URLSession.shared.dataTask(with: url) {(data: Data?, response: URLResponse?, error: Error?) in
guard let data = data else { return }
print(String(data: data, encoding: .utf8)!)
semphare.signal()
}
task.resume()
}
HTTP_Request()
_ = semphare.wait(timeout: .distantFuture)
This works for me many times I suggest you snippet for future uses!
let url = URL(string: "https://google.com")
let task = URLSession.shared.dataTask(with: ((url ?? URL(string: "https://google.com"))!)) { [self] (data, response, error) in
do {
let jsonResponse = try JSONSerialization.jsonObject(with: data!, options: [])
print(jsonResponse)
guard let newValue = jsonResponse as? [String:Any] else {
print("invalid format")
}
}
catch let error {
print("Error: \(error)")
}
task.resume()
}
I am trying to communicate with Swift to a php-website using the command "uploadTask". The site is sending Data back, which is working well. The result from the website is stored in the variable "answer". But how can I actually use "answer" AFTER the uploadTask.resume() was done?
When running the file, it always prints:
"One" then "three" then "two".
I know that I could do things with "answer" right where the section "print("two")" is. And at many examples right there the command "DispatchQueue.main.async { ... }" is used. But I explicitly want to finish the uploadTask and then continue with some more calculations.
func contactPHP() {
print("One")
let url = "http://....php" // website to contact
let dataString = "password=12345" // starting POST
let urlNS = NSURL(string: url)
var request = URLRequest(url: urlNS! as URL)
request.httpMethod = "POST"
let dataD = dataString.data(using: .utf8) // convert to utf8 string
URLSession.shared.uploadTask(with: request, from: dataD)
{
(data, response, error) in
if error != nil {
print(error.debugDescription)
} else {
let answer = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)!
print("Two")
}
}.resume() // Starting the dataTask
print("Three")
// Do anything here with "answer"
}
extension NSMutableData {
func appendString(string: String) {
let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
append(data!)
}
}
I already tried it with a completion handler. But this does not work either. This also gives me "One", "Four", "Two", "Three"
func test(request: URLRequest, dataD: Data?, completion: #escaping (NSString) -> ()) {
URLSession.shared.uploadTask(with: request, from: dataD)
{
(data, response, error) in
if error != nil {
print(error.debugDescription)
} else {
let answer = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)!
print("Two")
completion(answer)
}
}.resume() // Starting the dataTask
}
let blubb = test(request: request, dataD: dataD) { (data) in
print("Three")
}
print("Four")
Use the URLSession function that has the completion handler:
URLSession.shared.uploadTask(with: URLRequest, from: Data?, completionHandler: (Data?, URLResponse?, Error?) -> Void)
Replace your uploadTask function with something like this:
URLSession.shared.uploadTask(with: request, from: dataD) { (data, response, error) in
if let error = error {
// Error
}
// Do something after the upload task is complete
}
Apple Documentation
After you create the task, you must start it by calling its resume()
method. If the request completes successfully, the data parameter of
the completion handler block contains the resource data, and the error
parameter is nil.
If the request fails, the data parameter is nil and
the error parameter contain information about the failure. If a
response from the server is received, regardless of whether the
request completes successfully or fails, the response parameter
contains that information.
When the upload task is complete, the completion handler of the function is called. You could also implement the delegate's optional func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) function.
This code works perfectly when downloading one file. However when trying to download multiple files that contain data and caching the incoming data causes a mess. Since the download occurs non-stop, thus one file is done and the next starts. I can't cache them since I don't know which data belongs to which file.
lazy var downloadQueue: OperationQueue = {
var queue = OperationQueue()
queue.maxConcurrentOperationCount = 1000000
queue.name = "Files"
return queue
}()
func fetch(url: String, completionHandler: #escaping ([String:String]) -> (), completionHandlerQueue: OperationQueue?) {
let task = session.dataTask(with: URL(string: url)!, completionHandler: {
(data, response, error) in
guard let data = data, let type = String(data: data, encoding: String.Encoding.utf8), error == nil else {
print("Error with the data: \(error.debugDescription)")
return
}
guard let statusCode = (response as? HTTPURLResponse)?.statusCode, statusCode >= 200 && statusCode <= 299 else {
return
}
guard completionHandlerQueue != nil else {
return
}
completionHandlerQueue!.addOperation(BlockOperation(block: {
completionHandler(type)
}))
})
task.resume()
}
// Here is the links
let urls = [.....]
func start() {
for url in urls {
print("Start")
fetch(url: url, completionHandler: { type in
// The incoming data I don't know which belongs to which url.
}, completionHandlerQueue: downloadQueue)
}
print("End")
}
Since its async I will get
Start
End
Then the data will come. How can I overcome this?
I'm trying to create a UIImage with one of Apple's new layered image files that's hosted on a remote server.
The sample code below downloads the lsr file correctly (the data var holds a value), but creating a new NSImage with it results in a nil value. Ignore the fact that this code is synchronous and inefficient.
if let url = NSURL(string: "http://path/to/my/layered/image.lsr") {
if let data = NSData(contentsOfURL: url) {
let image = UIImage(data: data) // `image` var is nil here
imageView?.image = image
}
}
Any thoughts on how to download an LSR and create a UIImage with it?
That's how i solved it:
Convert you .lsr file to a .lcr file doing this from console:
xcrun --sdk appletvos layerutil --c your_file.lsr
Upload your_file.lcr on your server
Put these two functions into an util class:
func getDataFromUrl(url:NSURL, completion: ((data: NSData?, response: NSURLResponse?, error: NSError? ) -> Void)) {
NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) in
completion(data: data, response: response, error: error)
}.resume()
}
func downloadImage(url: NSURL, imageView: UIImageView){
print("Started downloading \"\(url.URLByDeletingPathExtension!.lastPathComponent!)\".")
getDataFromUrl(url) { (data, response, error) in
dispatch_async(dispatch_get_main_queue()) { () -> Void in
guard let data = data where error == nil else { return }
print("Finished downloading \"\(url.URLByDeletingPathExtension!.lastPathComponent!)\".")
imageView.image = UIImage(data: data)
}
}
}
Use it like this:
if let checkedUrl = NSURL(string: "http://domain/path/to/your_file.lcr") {
self.my_ui_view.contentMode = .ScaleAspectFit
downloadImage(checkedUrl, imageView: self.my_ui_view.contentMode)
}
This will use the image without saving it into the document directory, if you need that solution, ask me and i'll share.
I want to make a method with URL parameter that returns the response of calling that URL.
How can I return the data obtained inside a completion block for a method?
class func MakeGetRequest(urlString: String) -> (data: NSData, error: NSError)
{
let url = NSURL(string: urlString)
var dataResponse: NSData
var err: NSError
let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
//How can I return the data obtained here....
})
task.resume()
}
If you want the MakeGetRequest method to return data obtained via dataTaskWithURL, you can't. That method performs an asynchronous call, which is most likely completed after the MakeGetRequest has already returned - but more generally it cannot be know in a deterministic way.
Usually asynchronous operations are handled via closures - rather than your method returning the data, you pass a closure to it, accepting the parameters which are returned in your version of the code - from the closure invoked at completion of dataTaskWithURL, you call that completion handler closure, providing the proper parameters:
class func MakeGetRequest(urlString: String, completionHandler: (data: NSData, error: NSError) -> Void) -> Void
{
let url = NSURL(string: urlString)
var dataResponse: NSData
var err: NSError
let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
completionHandler(data: data, error: error)
})
task.resume()
}
Swift 5 update:
class func makeGetRequest(urlString: String, completionHandler: #escaping (Data?, Error?) -> Void) -> Void {
let url = URL(string: urlString)!
var dataResponse: Data
var err: NSError
let task = URLSession.shared.dataTask(with: url, completionHandler: { (data, respone, error) -> Void in
completionHandler(data, error)
})
task.resume()
}