iTunes Search API JSON Fetch
AppStore JSON APIs
Now that we have the foundation of our UI set up in AppsSearchController, we're ready to integrate some JSON data by making some API calls to the iTunes Search API. I'll first go through how the data set can be fetched and interpreted through the browser. Once we have a good understanding of what can be returned, I'll walk through step by step on how to fetch this data in our project. One of the best things was introduced in Swift 3 is the Decodable protocol and allows JSON data to be very quickly parsed into Swift model objects. I'll show you how to set this up in this lesson and how to populate our cells in the next video.

Comments (18)
Viswa Kodela
4 years ago
Hey Brian, I have a small question, instead of getting all the results at once, is there any possibility that we can able to get some data first and then get the another set of data when the user starts scrolling, It will help to reduce the usage of the user data rite.
Brian Voong
4 years ago
Viswa Kodela
4 years ago
It would be very nice if you include pagination technique in this series.
Tube
4 years ago
Second the motion.
serxhio
4 years ago
I am VERY interested! I'm trying to do something similar with a different API and I can't! Please involve pagination!
PreachOnBerto
4 years ago
Would be cool to see a pagination technique using UICollectionViewDataSourcePrefetching
Viswa Kodela
4 years ago
I also heard a lot of nice things that we can do with willDisplay, forItemAt indexPath method. if there is any possibility, can you explain about this method, this way we can able to render the views inside the cells, before they appear on to the screen, and also reduces the cloggy scrolling when user scrolls fast. I am trying to implement it in one of my project, but I don't find any thing online that I can get satisfied.
tatters
4 years ago
Hi Brian, I get the following error: Sending selectors failed with: Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service named com.apple.commcenter.coretelephony.xpc was invalidated." UserInfo={NSDebugDescription=The connection to service named com.apple.commcenter.coretelephony.xpc was invalidated. The print statement - print(String(data: data!, encoding: .utf8)) - is working and returning the json data from the Apple service however it errors out when I replace that print statement with the "do" block of code? do { let searchResult = try JSONDecoder().decode(SearchResult.self, from: data) searchResult.results.forEach({print($0.trackName, $0.primaryGenreName)}) } catch let jsonErr { print("Failed to decode json:", jsonErr) }
tatters
4 years ago
I think it's a simulator issue? I'll try a real device
richardbranson
4 years ago
I often forget to call that .resume() on my URLSession. Now, I know that I am not the only one doing it. BTW Thanks for the lessons.
Tube
4 years ago
How do you form a URL to get the lists available in the new AppStore, e.g., Trending on the Search page or Top Free Apps on the Apps page? There are quite a few of these special lists, but I don't see how to request them. (I think these web APIs like iTunes and YouTube are awful. What in the world are they optimizing? Their SQL?)
Brian Voong
4 years ago
Yunio
4 years ago
hey Brian first of thanks for this course yet another great one can I do this with son decoder Submit a POST method to: a url Parameters are: username: password: iif it authenticates ok I get the son token than to see if it works I need to pass the token in headers if the token is valid it finally decodes with the user profile api? can this be accomplish with son decoder??
ezechinwa
4 years ago
Hi Brian, Do you have any resources that can help beginners like us on performing Post, Put requests, also adding headers to request calls using this library? And secondly, I don't intend to deviate from the subject of discussion but I was wondering Why is using URLSession better than using third-party request library like Alamofire?
Brian Voong
4 years ago
ezechinwa
4 years ago
:) Thanks Brian
ZiadHamdieh
4 years ago
Yes please!!
tonytech
4 years ago
if any of guys have problems parsing the json like me. these have to be optional. let trackName: String? let primaryGenreName: String?
Ante Stipaničev
4 years ago
Why are you not using Insomnia ( https://insomnia.rest/ ), Postman or some other application for API dev/debug?
Brian Voong
4 years ago
Alex Grinberg
4 years ago
HI Brian, Maybe I am missing something, but when trying to fetch the JSON I get this compiler error : Instance method 'decode(_:from:)' requires that 'SearchResult' conform to 'Decodable' Do I need to initialise something? What I am missing?
Alex Grinberg
4 years ago
I think I managed to fix. At SearchResult class, I conform it to Decoder. Hope it works at the end :)
Brian Voong
4 years ago
stonypig1
4 years ago
hello Brian: those structs, you use let, can we use var and adding optional at the end, maybe sometime one label field can't get back successfully due network data lose or error by human. would it be more crash proof or my concern is just redundant and no need to do it. thanks
Brian Voong
4 years ago
ramsayl
4 years ago
Hi Brian, this is a nice demonstration. However, do you cover fetching api results while an api key is required in this series?
Connor Laganà
4 years ago
Hey Brian, how would you go upon using the Decodable process for parallel arrays?
Brian Voong
4 years ago
christiaan2211
4 years ago
Hi Brian, first of all... your courses are AMAZING, I'm a big fan. You've helped me out a lot with the content in this course as well as your instagram firebase course. I am working on an app will have dynamic data that I would like to fetch in a similair fashion as you're doing here. How would I go about doing this? My client would want to be able to add data / objects to the data themselves. Do you have any recommendations? A CMS with json api generator? I'm not experienced in creating my own setups like this.
Brian Voong
4 years ago
christiaan2211
4 years ago
Quick project description: I'm creating an app in which I'm using the ig-firebase course as an example to handle login/signup and once the user is logged in we want to show them certain recipes (alcoholic drinks production related). I am using the 'today animations' as inspirations on how to load/open the recipes. The goal is to have the recipes be loaded in from a JSON fetch, but I'm not very familiar with back-end development...
hcri1950
4 years ago
Hi Brian, I love you course and I have learned so much in such a short time from your two courses that i bought. You mentioned earlier about a video on Decodable a short while ago. Could you embed the mention of that in your Module header please. This would be very handy to go back and refer to it from time to time. DEEPLY APRICIATE FOR THE TIME AND EFFORT that you have devoted yourself to creating this course.
Brian Voong
4 years ago
hcri1950
4 years ago
thank you very much
Jason18401
4 years ago
Can someone tell me what that fileprivate does? still a bit confused. Thanks!
Brian Voong
4 years ago
Jason18401
4 years ago
Thanks Brian!
Lazaro Ambrosio
3 years ago
No questions I just wanted to say thank you for the time and effort you created this course. It is so good! I can not wait to complete it . Cheers !
Brian Voong
3 years ago
fkeebie
3 years ago
I have finished your course up to lesson 53 and started again your course from lesson 1 up this time with Xcode 11.3.1. Your lessons give me undiminished excitement to learn.
Brian Voong
3 years ago
fkeebie
3 years ago
Thanks for your suggestion. Looks like a very interesting challenge! I'd like to give it a try when I've finished the review.
fkeebie
3 years ago
The review is nothing but the repetition of what I did, so I restarted to code AppsSearchController utilizing Compositional Layout. Going through a bit of trial and error, I think I am now deepening my understanding and recognize again it works like a magic wand.
HELP & SUPPORT