Advertisement
Guest User

JSON

a guest
Oct 17th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. import UIKit
  2. import WebKit
  3.  
  4. class ViewController: UITableViewController {
  5.  
  6.  
  7. struct TopStoriesResponse: Decodable {
  8. let status: String
  9. let results: [Story]
  10. }
  11.  
  12. struct Story: Decodable {
  13. let title: String
  14. let abstract: String
  15. let url: String
  16. }
  17.  
  18. var headlines = [String]()
  19. var abstracts = [String]()
  20. var images = [String]()
  21. var urls = [URL]()
  22. var webView: WKWebView!
  23.  
  24. override func viewDidLoad() {
  25. super.viewDidLoad()
  26.  
  27. webView = WKWebView()
  28. getJson { (success) in
  29. print("Success")
  30. }
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37. func getJson(completionHandler: @escaping (Bool) -> ()) {
  38. let jsonUrlString = "https://api.nytimes.com/svc/topstories/v1/business.json?api-key=f4bf2ee721031a344b84b0449cfdb589:1:73741808"
  39. guard let url = URL(string: jsonUrlString) else {return}
  40.  
  41. URLSession.shared.dataTask(with: url) { (data, response, err) in
  42. guard let data = data, err == nil else {
  43. print(err!)
  44. return
  45. }
  46.  
  47. do {
  48. let response = try
  49. JSONDecoder().decode(TopStoriesResponse.self, from: data)
  50. print(response.results)
  51.  
  52. // Pass results into arrays (title, abstract, url, image)
  53. completionHandler(true)
  54.  
  55. DispatchQueue.main.async {
  56. self.tableView.reloadData()
  57. }
  58. } catch let jsonErr {
  59. print("Error serializing JSON", jsonErr)
  60. }
  61. }.resume()
  62. }
  63.  
  64.  
  65.  
  66. // MARK: - Tableview methods
  67.  
  68. override func numberOfSections(in tableView: UITableView) -> Int {
  69. return 1
  70. }
  71.  
  72. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  73. return headlines.count
  74. }
  75.  
  76. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  77. let cell = tableView.dequeueReusableCell(withIdentifier: "JSONcell", for: indexPath) as! JSONTableViewCell
  78.  
  79. // cell.cellHeadlineLabel.text = self.headlines[indexPath.row]
  80. // cell.cellDetailLabel.text = self.abstracts[indexPath.row]
  81. // cell.cellImageView.cacheImage(urlString: self.images[indexPath.row])
  82.  
  83. return cell
  84. }
  85.  
  86. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  87.  
  88. webView.load(URLRequest(url: self.urls[indexPath.row]))
  89. webView.allowsBackForwardNavigationGestures = true
  90. view = webView
  91.  
  92. // Back button
  93. // self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.goBack))
  94. }
  95.  
  96. func goBack() {
  97. if webView.canGoBack {
  98. webView.goBack()
  99. }
  100. }
  101.  
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement