0

So I am fairly new to working with iOS applications and I am currently working on an application which pulls data from a website using NSURLSessionDataTask, parses the JSON response (HTML), and then populates a view with data from the response.

Currently, I am stuck trying to find a solution to correctly parsing my current HTML string (of type __NSCFString/NSString) from the JSON response, and putting the text into one or more UITextViews and images into one or more UIImageViews within the main ViewController.

It has previously been suggested to simply use a UIWebView to display everything or to use an outside library to do some of the converting, however I am curious if there are any methods by which I could parse the HTML and extract all relevant text or images and throw them into an array for later use. I would very much prefer to keep all of this native and not use outside libraries unless absolutely necessary.

I have also seen some use of the NSAttributedString class for parsing HTML from a JSON response, but am unsure if this is even relevant to what I am trying to do here. Any help/suggestions/thoughts are appreciated!

Community
  • 1
  • 1
Jeremy Dennen
  • 172
  • 1
  • 10
  • I do not understand your question. What exactly is the problem with setting a string into UITextView? – rocky Nov 04 '15 at 21:42
  • @rocky My current string is a bunch of HTML pulled directly from the website. Therefore, setting this string to the text of a UITextView will display a bunch of HTML to the user - not ideal. – Jeremy Dennen Nov 06 '15 at 19:18
  • A simple google will lead you to a bunch of hits on how to parse HTML. Here's the first hit. http://www.raywenderlich.com/14172/how-to-parse-html-on-ios – rocky Nov 06 '15 at 19:28
  • @rocky as previously mentioned I am looking for a native solution to extracting the text and photos. Just about everything I have found, including your link, rely on outside libraries. For the time being I am working with `NSAttributedString`, however it's proving difficult to work with and not grabbing all of the necessary images. – Jeremy Dennen Nov 10 '15 at 22:25

1 Answers1

0

You can use the native NSXMLParser class. Checkout the documentation here.

Within your custom parsing class. You can generate NSAttributedString and store into array based on custom logic. This should help.

Community
  • 1
  • 1
Si Te Feng
  • 135
  • 1
  • 14
  • I don't think this is a solution I can use, as I am trying to parse HTML not XML and it looks like [`NSXMLParser` doesn't work with regular HTML](http://stackoverflow.com/questions/14105972/can-nsxmlparser-be-use-to-parse-html?lq=1) – Jeremy Dennen Nov 06 '15 at 19:27