How do I display more JSON items in HTML using WebKit?

forrest

I am building a news app using SwiftyJSON and I have been able to pull the data properly. I am also able to display the title and description in a tableView. However, when I go to the Detail View I want to be able to display the full article summary from the feed.

Here are the feed elements:

func parse(json: JSON) {
        for article in json["articles"].arrayValue {
            let title = article["title"].stringValue
            let author = article["author"].stringValue
            let date = article["publishedAt"].stringValue
            let image = article["urlToImage"].stringValue
            let description = article["description"].stringValue

            let obj = ["title": title, "author": author, "date": date, "image": image, "description": description, ]
            news.append(obj)

        }

I am sending the data to the Detail View Controller as follows:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = DetailViewController()
        vc.articleSummary = news[indexPath.row]
        navigationController?.pushViewController(vc, animated: true)
}

Then on the Detail View Controller here is the code. The commented items are items that I would like to add to the display:

import UIKit
import WebKit

class DetailViewController: UIViewController {

    var webView: WKWebView!
    var articleSummary: [String: String]!

    override func loadView() {
        webView = WKWebView()
        view = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        guard articleSummary != nil else { return }

        if let description = articleSummary["description"] {
            var html = "<html>"
            html += "<head>"
            html += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
            html += "<style> body { font-size: 150%; } </style>"
            html += "</head>"
            html += "<body>"
         // html += <h1>title</h1>
         // html += <h4>author</h4>
         // html += <p>date</p>
         // html += <img src="image" alt="" />
            html += description
            html += "</body>"
            html += "</html>"
            webView.loadHTMLString(html, baseURL: nil)
        }
    }

}
Christian Abella

You just need to escape the HTML string properly and your data will be displayed.

if let description = articleSummary["description"],
   let title = articleSummary["title"],
   let author = articleSummary["author"],
   let image = articleSummary["image"],
   let date = articleSummary["date"] {
        var html = "<html>"
        html += "<head>"
        html += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
        html += "<style> body { font-size: 150%; } </style>"
        html += "</head>"
        html += "<body>"
        html += "<h1>\(title)</h1>"
        html += "<h4>\(author)</h4>"
        html += "<p>\(date)</p>"
        html += "<img src=\"\(image)\" alt=\"\" />"
        html += description
        html += "</body>"
        html += "</html>"
    webView.loadHTMLString(html, baseURL: nil)
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do i display my items in a row ( using php and bootstrap to display all items from a database)

How do I pull items from JSON file to HTML code?

How do I query certain JSON items using Retrofit?

How do I display JSON data with HTML tags applied, in React?

How do I display Json API data in html?

How do I display an HTML file using Websphere Liberty?

How do I make this HTML display as a table using CSS floats?

How do I display json data using Reactjs?

how do I display json results using jquery ajax

How do I display external JSON using Pug/Jade?

How do I filter an unorderded list to display only selected items using Javascript?

ansible: how do I display the output from a task that using with_items?

How do I display list of items horizontally on a list View, using WPF?

FlatList using 2 columns. I have an odd number of items to display. How do I get the last item to align left?

how do i display more than 1 item with javascript

How do I display more rows of a Dataframe in Jupyter Notebook?

How do I access items of a dictionary using items from a tuple?

How do I fix display of items in dropdown list?

How do I display picker control items within a custom control?

How do i display List<object> with items in console C#

How do I add items to array in following format and display it in flatlist?

How do I display database items separated by an ID in Coldfusion?

How do I display number of selected items in checkbox?

How do I retrieve / compare items in an HTML Listbox using C#

How do I parse specific items from a HTML page using java?

How do I display a wrapped json string?

How do I get and display JSON with HTTParty?

How to display json data in html using javascript?

How to display HTML received in JSON using React

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive