I have the Master-Detail project where I am parsing data from JSON. The purpose is to add UIActivityIndicatorView (by using URLSession) to the DetailsViewController while waiting for data be fetched and loaded to the DetailsViewController. I have tried several ways by starting UIActivityIndicatorView in Master after the following:
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
Also I do not know where to stop it, I have tried it in ViewDidLoad() of the DetailViewController (before configureView()):
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
configureView()
}
But also did not work. I could not find anywhere the information about adding activity indicator by using the state of URLSession. Here I add the code from MasterViewController where I have tried to start the activity indicator:
let arrayOfUrls = [ URL(string: "http://www.omdbapi.com/?t=The+Dark+Knight&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Lord+of+the+Rings%3A+The+Return+of+the+King&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=Forrest+Gump&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=Inception&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Matrix&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=Interstellar&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Pianist&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Intouchables&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Departed&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Prestige&apikey=f85dc75e") ]
for url in arrayOfUrls {
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if let error = error {
print (error)
} else {
if let data = data {
do {
let movie = try JSONDecoder().decode(Movie.self, from: data)
print(movie.Title)
self.objects.append(movie.Title)
self.details.append(movie)
} catch {
print("Json Processing Failed")
}
}
}
}
task.resume()
}
}
Create NetworkService class and do api calls in a func, this way much better.
class NetworkService {
let arrayOfUrls = [ URL(string: "http://www.omdbapi.com/?t=The+Dark+Knight&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Lord+of+the+Rings%3A+The+Return+of+the+King&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=Forrest+Gump&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=Inception&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Matrix&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=Interstellar&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Pianist&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Intouchables&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Departed&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Prestige&apikey=f85dc75e") ]
func getData(completion:#escaping(Movie)->()){
for url in arrayOfUrls {
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
var movie = Movie()
if let error = error {
print (error)
} else {
if let data = data {
do {
movie = try JSONDecoder().decode(Movie.self, from: data)
print(movie.Title)
self.objects.append(movie.Title)
self.details.append(movie)
} catch {
print("Json Processing Failed")
}
}
}
completion(movie)
}
task.resume()
}
}
}
In view Controller call your func:
let networkService = NetworkService()
activityIndicator.startAnimating()
networkService.getData { result in
self.activityIndicator.stopAnimating()
//result your movie data do whatever yo want it
DispatchQueue.main.async {
//If you need to reload tableview or etc. do here
}
}
In case I want to check if the file exists on my iPhone, just use the following code:
let filePath = fileName.path
let fileManager = FileManager.default
if fileManager.fileExists (atPath: filePath) {
}
How can I check if there is a pdf / jpg / png file at the URL:
www.myname.com/files/file1.jpg or www.myname.com/files/file2.pdf etc.?
Could I ask for an example of such a function - but for files on internet web servers?
UPDATE
func remoteFileExistsAt(url: URL, completion: #escaping (Bool) -> Void) {
let checkSession = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "HEAD"
request.timeoutInterval = 1.0 // Adjust to your needs
let task = checkSession.dataTask(with: request) { (data, response, error) -> Void in
if let httpResp = response as? HTTPURLResponse {
completion(httpResp.statusCode == 200)
}
}
task.resume()
}
Is it possible to check in this function whether the file is of JPG or PNG type? If so - then we also return true, and if not, false?
Updated:
After the discussion on the comments section, the code is updated to work in more correct way.
You should check for the mimeType of the URLResponse object rather than checking whether the image could be represented as UIImageJPEGRepresentation/UIImagePNGRepresentation or not. Because it doesn't guarantee that the resource is actually a jpg/jpeg or png.
So the mimeType should be the most reliable parameter that needs to considered here.
enum MimeType: String {
case jpeg = "image/jpeg"
case png = "image/png"
}
func remoteResource(at url: URL, isOneOf types: [MimeType], completion: #escaping ((Bool) -> Void)) {
var request = URLRequest(url: url)
request.httpMethod = "HEAD"
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
guard let response = response as? HTTPURLResponse, response.statusCode == 200, let mimeType = response.mimeType else {
completion(false)
return
}
if types.map({ $0.rawValue }).contains(mimeType) {
completion(true)
} else {
completion(false)
}
}
task.resume()
}
Verify with this:
let jpegImageURL = URL(string: "https://vignette.wikia.nocookie.net/wingsoffire/images/5/54/Panda.jpeg/revision/latest?cb=20170205005103")!
remoteResource(at: jpegImageURL, isOneOf: [.jpeg, .png]) { (result) in
print(result) // true
}
let pngImageURL = URL(string: "https://upload.wikimedia.org/wikipedia/commons/6/69/Giant_panda_drawing.png")!
remoteResource(at: pngImageURL, isOneOf: [.jpeg, .png]) { (result) in
print(result) //true
}
let gifImageURL = URL(string: "https://media1.tenor.com/images/f88f6514b1a800bae53a8e95b7b99172/tenor.gif?itemid=4616586")!
remoteResource(at: gifImageURL, isOneOf: [.jpeg, .png]) { (result) in
print(result) //false
}
Previous Answer:
You can check if the remote data can be represented as UIImageJPEGRepresentation or UIImagePNGRepresentation. If yes, you can say that remote file is either JPEG or PNG.
Try this:
func remoteResource(at url: URL, isImage: #escaping ((Bool) -> Void)) {
let request = URLRequest(url: url)
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let data = data, let image = UIImage(data: data) {
if let _ = UIImageJPEGRepresentation(image, 1.0) {
isImage(true)
} else if let _ = UIImagePNGRepresentation(image) {
isImage(true)
} else {
isImage(false)
}
} else {
isImage(false)
}
}
task.resume()
}
Usage:
let imageURL = URL(string: "http://domaind.com/index.php?action=GET_PHOTO&name=102537.jpg&resolution=FHD&lang=PL®ion=1")!
remoteResource(at: imageURL) { (isImage) in
print(isImage) // prints true for your given link
}
It’s about getting data from an URL. If the data is nil the file don’t exist.
Getting data from an URL in Swift
If you want to know the file exists on a server, then it requires sending a HTTP request and receiving the response.
Please try with following code,
func remoteFileExistsAt(url: URL, completion: #escaping (Bool) -> Void) {
let checkSession = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "HEAD"
request.timeoutInterval = 1.0 // Adjust to your needs
let task = checkSession.dataTask(with: request) { (data, response, error) -> Void in
if let httpResp = response as? HTTPURLResponse {
completion(httpResp.statusCode == 200)
} else {
completion(false)
}
}
task.resume()
}
UPDATE
remoteFileExistsAt(url: URL(string: "http://domaind.com/index.php?action=GET_PHOTO&name=102537.jpg&resolution=FHD&lang=PL®ion=1")!) { (success) in
print(success)
}
If I put this URL into a browser, the 206 KB image comes back in 2 seconds.
Why doesn't this call complete the download?
The data returned has response 200 which I thought meant that the http call had completed.
//: Playground - noun: a place where people can play
import UIKit
import PlaygroundSupport
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
func getImageFromURL(_ imageURL: String, completion: #escaping (UIImage?) -> Void) {
if let url = URL(string: imageURL) {
print("Download: \(imageURL)")
let urlRequest = URLRequest(url: url, timeoutInterval: 29.0)
session.dataTask(with: urlRequest) { (data: Data?, response: URLResponse?, error: Error?) -> Void in
guard let response = response, let data = data else {
completion(nil)
print("Failed to load image.")
return
}
print("Image data is \(data)")
let downloadedImage = UIImage(data: data)
DispatchQueue.main.async {
completion(downloadedImage)
print("Downloaded image size: \(downloadedImage?.size)")
}
}.resume()
}
}
PlaygroundPage.current.needsIndefiniteExecution = true
let theUrlString: String = "http://www.impawards.com/2014/posters/american_sniper_xlg.jpg" // 206 KB image
var theImage: UIImage = UIImage()
getImageFromURL(theUrlString) { image in
theImage = image!
}
print("theImage size: \(theImage.size)")
Hello I have working json parsing code for swift2.2 but when i use it for Swift 3.0 gives me that error
ViewController.swift:132:31: Ambiguous reference to member 'dataTask(with:completionHandler:)'
My code is here:
let listUrlString = "http://bla.com?batchSize=" + String(batchSize) + "&fromIndex=" + String(fromIndex)
let myUrl = URL(string: listUrlString);
let request = NSMutableURLRequest(url:myUrl!);
request.httpMethod = "GET";
let task = URLSession.shared().dataTask(with: request) {
data, response, error in
if error != nil {
print(error!.localizedDescription)
DispatchQueue.main.sync(execute: {
AWLoader.hide()
})
return
}
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSArray
if let parseJSON = json {
var items = self.categoryList
items.append(contentsOf: parseJSON as! [String])
if self.fromIndex < items.count {
self.categoryList = items
self.fromIndex = items.count
DispatchQueue.main.async(execute: {
self.categoriesTableView.reloadData()
AWLoader.hide()
})
}else if( self.fromIndex == items.count){
DispatchQueue.main.async(execute: {
AWLoader.hide()
})
}
}
} catch {
AWLoader.hide()
print(error)
}
}
task.resume()
Thanks for ideas.
The compiler is confused by the function signature. You can fix it like this:
let task = URLSession.shared.dataTask(with: request as URLRequest) {
But, note that we don't have to cast "request" as URLRequest in this signature if it was declared earlier as URLRequest instead of NSMutableURLRequest:
var request = URLRequest(url:myUrl!)
This is the automatic casting between NSMutableURLRequest and the new URLRequest that is failing and which forced us to do this casting here.
You have init'd myRequest as NSMutableURLRequest, you need this:
var URLRequest
Swift is ditching both the NSMutable... thing. Just use var for the new classes.
Xcode 8 and Swift 3.0
Using URLSession:
let url = URL(string:"Download URL")!
let req = NSMutableURLRequest(url:url)
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config, delegate: self, delegateQueue: OperationQueue.main)
let task : URLSessionDownloadTask = session.downloadTask(with: req as URLRequest)
task.resume()
URLSession Delegate call:
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask,
didWriteData bytesWritten: Int64, totalBytesWritten writ: Int64, totalBytesExpectedToWrite exp: Int64) {
print("downloaded \(100*writ/exp)" as AnyObject)
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL){
}
Using Block GET/POST/PUT/DELETE:
let request = NSMutableURLRequest(url: URL(string: "Your API URL here" ,param: param))!,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval:"Your request timeout time in Seconds")
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers as? [String : String]
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest) {data,response,error in
let httpResponse = response as? HTTPURLResponse
if (error != nil) {
print(error)
} else {
print(httpResponse)
}
DispatchQueue.main.async {
//Update your UI here
}
}
dataTask.resume()
Working fine for me.. try it 100% result guarantee
This problem is caused by URLSession has two dataTask methods
open func dataTask(with request: URLRequest, completionHandler: #escaping (Data?, URLResponse?, Error?) -> Swift.Void) -> URLSessionDataTask
open func dataTask(with url: URL, completionHandler: #escaping (Data?, URLResponse?, Error?) -> Swift.Void) -> URLSessionDataTask
The first one has URLRequest as parameter, and the second one has URL as parameter, so we need to specify which type to call, for example, I want to call the second method
let task = URLSession.shared.dataTask(with: url! as URL) {
data, response, error in
// Handler
}
In my case error was in NSURL
let url = NSURL(string: urlString)
In Swift 3 you must write just URL:
let url = URL(string: urlString)
Tested xcode 8 stable version ; Need to use var request variable with URLRequest() With thats you can easily fix that (bug)
var request = URLRequest(url:myUrl!) And
let task = URLSession.shared().dataTask(with: request as URLRequest) { }
Worked fine ! Thank you guys, i think help many people. !
For Swift 3 and Xcode 8:
var dataTask: URLSessionDataTask?
if let url = URL(string: urlString) {
self.dataTask = URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in
if let error = error {
print(error.localizedDescription)
} else if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 {
// You can use data received.
self.process(data: data as Data?)
}
})
}
}
//Note: You can always use debugger to check error
In swift 3 the compiler is confused by the function signature. Specifying it will clear the error. Also convert the url string to type URL. The following code worked for me.
let urlString = "http://bla.com?batchSize="
let pathURL = URL(string: urlString)!
var urlRequest = URLRequest(url:pathURL)
let session = URLSession.shared
let dataTask = session.dataTask(with: urlRequest as URLRequest) { (data,response,error) in
Short and concise answer for Swift 3:
guard let requestUrl = URL(string: yourURL) else { return }
let request = URLRequest(url:requestUrl)
URLSession.shared.dataTask(with: request) {
(data, response, error) in
...
}.resume()
// prepare json data
let mapDict = [ "1":"First", "2":"Second"]
let json = [ "title":"ABC" , "dict": mapDict ] as [String : Any]
let jsonData : NSData = NSKeyedArchiver.archivedData(withRootObject: json) as NSData
// create post request
let url = NSURL(string: "http://httpbin.org/post")!
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
// insert json data to the request
request.httpBody = jsonData as Data
let task = URLSession.shared.dataTask(with: request as URLRequest){ data,response,error in
if error != nil{
return
}
do {
let result = try JSONSerialization.jsonObject(with: data!, options: []) as? [String:AnyObject]
print("Result",result!)
} catch {
print("Error -> \(error)")
}
}
task.resume()
To load data via a GET request you don't need any URLRequest (and no semicolons)
let listUrlString = "http://bla.com?batchSize=" + String(batchSize) + "&fromIndex=" + String(fromIndex)
let myUrl = URL(string: listUrlString)!
let task = URLSession.shared.dataTask(with: myUrl) { ...
let task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { data,response,error in
if error != nil{
print(error!.localizedDescription)
return
}
if let responseJSON = (try? JSONSerialization.jsonObject(with: data!, options: [])) as? [String:AnyObject]{
if let response_token:String = responseJSON["token"] as? String {
print("Singleton Firebase Token : \(response_token)")
completion(response_token)
}
}
})
task.resume()
Xcode 10.1 Swift 4
This worked for me:
let task: URLSessionDataTask = session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
...
The key was adding in the URLSessionDataTask type declaration.
For me I do this to find,
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!) { (data, response, error) in ...}
Can't use
"let url = NSURL(string: urlString)
I had a code in iOS 8 for loading my images, and it was quite fast.
if let url = NSURL(string: urlString) {
let request = NSURLRequest(URL: url)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {
(response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in
self.image = UIImage(data: data!)
}
}
Then iOS 9 came out and I struggle with this
let session = NSURLSession.sharedSession()
let urlString = urlString
let url = NSURL(string: urlString)
let request = NSURLRequest(URL: url!)
let dataTask = session.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
self.image = UIImage(data: data!)
}
dataTask.resume()
Images are loading very slow.
Inside the dataTaskWithRequest closure, you have to update the image on the main thread. Do it like this:
let dataTask = session.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
dispatch_async(dispatch_get_main_queue()) {
self.image = UIImage(data: data!)
}
}