简体   繁体   中英

How do I retrieve text and images from websites (in HTML or JSON) on iOS Swift?

I looked around the Internet to figure out how to get data from websites using SWIFT, and have narrowed it down to roughly JSON or HTML, which I am not familiar with.

As far as I know, there are third party libraries for me to use to parse the data. I have been following Dani Arnaout's Working with JSON in Swift Tutorial as a reference. However, I have not been able to find the way to retrieve the JSON from any random website. Only the iTunes JSON page, which is included in the tutorial, works.

What I want to do: Make an app that downloads images and also some text from many different websites, either by HTML or JSON. The problem right now is that I have no idea how to start doing it. A simple demo would be helpful.

Questions

  1. How do I get the JSON of any random website on the Internet?

  2. How do I make use of the HTML data from websites to turn it into a readable format? I'm retrieving the HTML using Google Chrome, and it seems to be gibberish: I can't find the text anywhere.

Question 1 : Not all websites expose their content formatted as JSON.

Question 2 : I think you should look at a couple of resources. First Ray Wenderlich has a tutorial on how to parse HTML . Although it is using Objective-C you should be able to learn quite a lot there.

When you have read that tutorial I would recommend you look at the Swift library Alamofire . There is another tutorial on the Wenderlich site covering this library.

Happy coding!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM