Reputation: 1488
I am trying to get stock quotes from Yahoo using Swift 3. Although there are some decent tutorials on Swift 2, none of them seem to translate well to Swift 3.
The issue I have at the moment is that in the code below, the session.dataTask is never called. The print statement never fires and the rest of the code that is in there does not work.
I have checked that the request variable looks good, and the url has been tested on the yahoo developer site.
So I'm thinking I must have the syntax of the dataTask wrong or has an error so is skipping completely.
Any thoughts?
urlString = "http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quotes where symbol IN ('APL')"
//let urlNSS : NSString = urlString as NSString
let urlStr : String = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
let url : URL = URL(string: urlStr as String)!
let request = URLRequest(url: url)
let session = URLSession.shared
let config = URLSessionConfiguration.default
let task = session.dataTask(with: request, completionHandler: {(data, response, error) -> Void in
print("in the task")
..
..
)}
task.resume()
If I inspect task I see the following
Upvotes: 3
Views: 3493
Reputation: 3752
You must resume the task (task.resume()) that you have created. By default, a task is in suspended state.
I have created a Playground file with a different Yahoo RSS Feed URL. https://drive.google.com/file/d/0B5nqEBSJjCriWl9UTWcxSE42Yk0/view?usp=sharing
The URL you have in your question is not giving any data.
<error xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:lang="en-US">
<description>No definition found for Table yahoo.finance.quotes</description>
</error>
Code as Below:
//: Playground - noun: a place where people can play
import UIKit
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
var str = "Hello, playground"
let yahooURLString = "https://feeds.finance.yahoo.com/rss/2.0/headline?s=yhoo®ion=US&lang=en-US"
let yahooRSSURL: URL = URL(string: yahooURLString)!
var request = URLRequest(url: yahooRSSURL)
request.httpMethod = "GET"
let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig)
let task = session.dataTask(with: request) {data, response, err in
print("Entered the completionHandler")
print("Response JSON:\n\(String(data: data!, encoding: String.Encoding.utf8)!)")
}
task.resume()
Hope this helps.
Edit: Attaching a screenshot of print statements appearing in the console of playgorund.
Upvotes: 4