Hello,
First of all, I dont know if I'm asking for too much.
I'm new in parsing API data in Swift. What I'm trying to do is to show in my two labels and UIImageView (or maybe there is other way to show album cover from external url) data from nowplaying api on my internet radio station.
Here how it is gonna look like in Main.storyboard:
So far I've tried couple of solutions, but I've stuck on parsing step. Here is my code:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
overrideUserInterfaceStyle = .light
setupRemoteCommandCenter()
//Radio API endpoint
let urlString = "https://admin.radiopromil.online/api/nowplaying_static/radio_promil.json"
let url = URL(string: urlString)
guard url != nil else {
return
}
let session = URLSession.shared
let dataTask = session.dataTask(with: url!) { (data, response, error) in
if error == nil && data != nil {
let decoder = JSONDecoder()
do {
let radio = try decoder.decode(radioAPI.self, from: data!)
print(radio)
DispatchQueue.main.async {
self.title.text = ????
self.artist.text = ????
}
}
catch {
print("Error Parsing JSON")
}
}
}
dataTask.resume()
}
}
So far I removed everything from radioAPI.swift. I
've got two API endpoint:
This one is static JSON https://admin.radiopromil.online/api/nowplaying_static/radio_promil.json
This one is standard API: https://admin.radiopromil.online/api/nowplaying/radio_promil
To sum up: I would like to parse data from API (now_playing: artist, title and cover art url) into two labels and UIImageView.
Data in API is changing when next song is playing.
Thank you in advance for your help.